[leetcode]The K Weakest Rows in a Matrix

jun·2021년 4월 11일
0
post-thumbnail

유의할점

compare 함수에 static이 들어가야함

풀이

binary search로 soldier의 수를 구하고 비교

코드

C++

class Solution {
public:
    static bool compare(pair<int,int> &a, pair<int,int> &b){
        if(a.first != b.first)
            return a.first < b.first;
        return a.second < b.second;
    }
    
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        int n = mat[0].size();
        vector< pair<int,int> > tmp;
        
        for(int i = 0 ; i < mat.size(); i++){
            int soldierN = n - (lower_bound(mat[i].rbegin(),mat[i].rend(),1) - mat[i].rbegin());
            tmp.push_back({soldierN,i});                    
        }
            
        sort(tmp.begin(),tmp.end(),compare);
        
        vector<int> res;
        for(int i = 0; i < k ; i++)
            res.push_back(tmp[i].second);
        return res;
    }
};
profile
Computer Science / Algorithm / Project / TIL

0개의 댓글