forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1337.cpp
More file actions
35 lines (33 loc) · 741 Bytes
/
1337.cpp
File metadata and controls
35 lines (33 loc) · 741 Bytes
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
typedef pair<int, int> PII;
class Solution
{
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k)
{
priority_queue<PII> q;
for (int i = 0; i < mat.size(); i++)
{
q.push({numOnes(mat[i]), i});
if (q.size() > k) q.pop();
}
vector<int> res(k);
for (int i = k - 1; ~i; i--)
{
res[i] = q.top().second;
q.pop();
}
return res;
}
private:
int numOnes(vector<int>& row)
{
int l = 0, r = row.size();
while (l < r)
{
int mid = (l + r) >> 1;
if (row[mid] == 1) l = mid + 1;
else r = mid;
}
return l;
}
};