Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Question link-https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/
- Answer Code-
- class Solution {
- public:
- vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
- //using vector pairs to pair the row number with the number of ones it contains
- vector<pair<int,int>> v;
- int c;
- for(int i=0;i<mat.size();i++)
- {
- c=0;//stores frequency
- for(int j=0;j<mat[i].size();j++)
- {
- if(mat[i][j]==1)
- {
- c++;
- }
- }
- v.push_back(make_pair(c,i));
- }
- vector<int> ans;
- sort(v.begin(), v.end());
- for (int i=0; i<k; i++)
- {
- ans.push_back(v[i].second);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment