-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path378.cpp
44 lines (37 loc) · 1.13 KB
/
378.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
__________________________________________________________________________________________________
sample 12 ms submission
static const int _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
int countLessEqualTarget(vector<vector<int>>& matrix, int target){
int n = matrix.size();
int i = n-1;
int j = 0;
int count = 0;
while(i>=0 && j<n){
if(matrix[i][j]<=target){
count += i+1;
j++;
}else {
i--;
}
}
return count;
}
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
int l = matrix[0][0];
int r = matrix.back().back();
int m;
while(l<r){
m = l + (r-l)/2;
countLessEqualTarget(matrix, m) < k ? l = m+1 : r = m;
}
return l;
}
};
__________________________________________________________________________________________________
__________________________________________________________________________________________________