C++ solution O(n^2)


  • 0
    Q
    class Solution {
    
    public:
        vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
            sort(people.begin(), people.end());
            for(int i = people.size()-1; i >=0; --i) {
                int count  = 0;
                for(int j = i-1; j >= 0 && people[j].first == people[i].first; ++count,--j) {
                }
                if(count != people[i].second) {
                    auto temp = people[i];
                    for(int j = 0; j < temp.second - count; ++j) {
                        people[i+j] = people[i+j+1];
                    }
                    people[i+ temp.second - count] = temp;
                }
            }
            return people;
        }
    };

Log in to reply
 

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