Skip to content

Commit

Permalink
Sync LeetCode submission - The K Weakest Rows in a Matrix (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheikh-arman committed Sep 18, 2023
1 parent 5bd7170 commit aad4a8d
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
using int2=pair<int, int>;
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
int n=mat.size();
priority_queue<int2> pq;
for(int i=0; i<k; i++){
int ones=accumulate(mat[i].begin(), mat[i].end(), 0);
pq.emplace(ones, i);
}

for(int i=k; i<n; i++){
int ones=accumulate(mat[i].begin(), mat[i].end(), 0);
int wt=pq.top().first;
if (ones<wt){
pq.emplace(ones, i);
pq.pop();
}
}
// cout<<pq.size()<<endl;
vector<int> ans(k, 0);
for(int i=k-1; i>=0; i--){
ans[i]=pq.top().second;
pq.pop();
}
return ans;
}
};

0 comments on commit aad4a8d

Please sign in to comment.