Skip to content

Commit 28eee63

Browse files
Create k_weakest_rows_in_matrix.cpp
1 parent c6b87cb commit 28eee63

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

k_weakest_rows_in_matrix.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
struct Comp {
4+
bool operator()(const pair<int, int> &l, const pair<int, int> &r) {
5+
if(l.first != r.first) {
6+
return l.first > r.first;
7+
}
8+
return l.second > r.second;
9+
}
10+
};
11+
12+
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
13+
if(mat.size() == 0 or k == 0) return {};
14+
15+
priority_queue<pair<int, int>, vector<pair<int, int>>, Comp > Q;
16+
unordered_map<int, int> count;
17+
18+
for(int r = 0; r < mat.size(); r++) {
19+
int countOnes = 0;
20+
for(int c = 0; c < mat[r].size(); c++) {
21+
if(mat[r][c] == 1) countOnes++;
22+
}
23+
count[r] = countOnes;
24+
}
25+
26+
for(auto &[k, v] : count) {
27+
Q.emplace(v, k);
28+
}
29+
30+
vector<int> result;
31+
while(k-- and !Q.empty()) {
32+
cout<<Q.top().first<<" "<<Q.top().second<<"\n";
33+
result.emplace_back(Q.top().second);
34+
Q.pop();
35+
}
36+
return result;
37+
}
38+
};

0 commit comments

Comments
 (0)