-
Notifications
You must be signed in to change notification settings - Fork 3
/
1337.cpp
28 lines (26 loc) · 818 Bytes
/
1337.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
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
int m = mat.size();
int n = mat[0].size();
priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> maxQ;
for (int i = 0; i < m; ++i) {
int left = 0;
int right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (mat[i][mid] == 0) right = mid;
else left = mid + 1;
}
maxQ.push({left, i});
if (maxQ.size() > k) maxQ.pop();
}
vector<int> res;
while (!maxQ.empty()) {
res.push_back(maxQ.top().second);
maxQ.pop();
}
reverse(res.begin(), res.end());
return res;
}
};