使用priority_queue幫忙排序,把小的挑出來,再開個vector把個數挑出來(其實可以不用heap,但我開薰><)
#define pii pair<int, int>
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
for(int i = 0 ; i < mat.size(); i++){
int tmp = 0;
for(int j = 0; j < mat[i].size(); j++){
tmp += mat[i][j];
}
pq.push({tmp, i});
}
vector<int> ans;
for(int i = 0; i < k; i++){
ans.push_back(pq.top().second);
pq.pop();
}
return ans;
}
};