jayati

Untitled

Aug 31st, 2022 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. Question link-https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/
  2.  
  3. Answer Code-
  4. class Solution {
  5. public:
  6. vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
  7. //using vector pairs to pair the row number with the number of ones it contains
  8. vector<pair<int,int>> v;
  9. int c;
  10. for(int i=0;i<mat.size();i++)
  11. {
  12. c=0;//stores frequency
  13. for(int j=0;j<mat[i].size();j++)
  14. {
  15. if(mat[i][j]==1)
  16. {
  17. c++;
  18. }
  19. }
  20. v.push_back(make_pair(c,i));
  21. }
  22. vector<int> ans;
  23. sort(v.begin(), v.end());
  24. for (int i=0; i<k; i++)
  25. {
  26. ans.push_back(v[i].second);
  27. }
  28. return ans;
  29.  
  30.  
  31. }
  32. };
Tags: Ques1
Advertisement
Add Comment
Please, Sign In to add comment