-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Kth Smallest Element in a Sorted Matrix.cpp
58 lines (52 loc) · 1.43 KB
/
Kth Smallest Element in a Sorted Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Company Tags : Google, Twitter
Leetcode Link : https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
*/
//Approach-1 (Naive Priority_queue approach O(n*nlog(k))
class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
priority_queue<int> pq;
for(vector<int>& vec : matrix) {
for(int &x : vec) {
pq.push(x);
if(pq.size() > k)
pq.pop();
}
}
return pq.top();
}
};
//Approach-2 (Binary Search : O(log(max-min) * n)
class Solution {
public:
int n;
//This function is in itself a Question (Find rank of an element in a sorted matrix) IMPORTANT
int lessThanEqualToCount(vector<vector<int>>& matrix, int target) {
int i = 0, j = n-1;
int count = 0;
//O(n)
while(i < n) {
while(j >= 0 && matrix[i][j] > target)
j--;
count += j+1;
i++;
}
return count;
}
int kthSmallest(vector<vector<int>>& matrix, int k) {
n = matrix.size();
int l = matrix[0][0];
int h = matrix[n-1][n-1];
//O(log(max-min))
while(l <= h) {
int mid = l + (h-l)/2;
int dist = lessThanEqualToCount(matrix, mid);
if(dist < k)
l = mid+1;
else
h = mid-1;
}
return l;
}
};