-
Notifications
You must be signed in to change notification settings - Fork 0
/
378_py.txt
58 lines (35 loc) · 1.12 KB
/
378_py.txt
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
class Solution
{
public:
int kthSmallest(vector<vector<int>>& m, int k)
{
int left = m[0][0];
int right = m.back().back();
while(left < right )
{
int mid = left + (right - left) / 2;
int count = lessthan(m,mid);
if(count < k)
left = mid + 1 ;
else
right = mid;
}
return left;
}
int lessthan(vector<vector<int>>& m, int target)
{
int i = m.size()-1, j = 0;
int res = 0;
while(i >= 0 && j < m.size())
{
if(m[i][j] <= target)
{
res += i+1; //该列以上行都小于target;
j++ ; // 扫描下一列;
}
else
i-- ; //上一行
}
return res;
}
};