-
Notifications
You must be signed in to change notification settings - Fork 0
/
The K Weakest Rows in a Matrix.cpp
55 lines (36 loc) · 1.37 KB
/
The K Weakest Rows in a Matrix.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
45
46
47
48
49
50
51
52
53
54
55
// You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.
// A row i is weaker than a row j if one of the following is true:
// The number of soldiers in row i is less than the number of soldiers in row j.
// Both rows have the same number of soldiers and i < j.
// Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.
class Solution {
public:
static bool comp(pair<int, int>a, pair<int, int> b){
if(a.second<b.second){
return true;
}
if(a.second==b.second) return a.first<b.first;
return false;
}
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
vector<pair<int, int>> store;
vector<int> ans;
int ind =0;
for(auto it: mat){
int temp =0;
for(auto p : it){
if(p==1) temp++;
}
store.push_back(make_pair(ind, temp));
ind++;
}
sort(store.begin(), store.end(), comp);
int i=0;
while(k--){
ans.push_back(store[i].first);
i++;
}
// for(auto it: store) cout<<it.first<<" "<<it.second<<endl;
return ans;
}
};