c++ solution


  • 0
    B
    class Solution {
    public:
        vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
            int n=people.size();
            if(!n) return vector<pair<int, int>>();
            
            sort(people.begin(), people.end());
            vector<pair<int, int>> result(n, make_pair(-1,-1));
            
            int t=0, cnt=0;
            for(int i=0;i<n;i++){
                if(i==0 || people[i].first>people[i-1].first) {
                    cnt=0;
                    t=findplace(result, people[i].second);
                }
                else {
                    cnt++;
                    t=findplace(result, people[i].second-cnt);
                }
                result[t]=people[i];
            }
            
            return result;
        }
        
        int findplace(vector<pair<int, int>> result, int n){
            for(int i=0;i<result.size();i++){
                if(result[i].second==-1) n--;
                if(n<0) return i;
            }
            return result.size()-1;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.