Share my C++ solution, easy to understand


  • 1
    V
    class Solution {
    public:
        static bool myComp(const pair<int, int> &a, const pair<int, int> &b)
        {
            if (a.first == b.first)
                return a.second < b.second;
                
            return a.first > b.first;
        }
        
        vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
            vector<pair<int, int>> ret;
            int n = people.size();
            if (n == 0)
                return ret;
                
            sort(people.begin(), people.end(), myComp);
            
            for (int i = 0; i < n; i++)
            {
                ret.insert(ret.begin() + people[i].second, people[i]);
            }
            
            return ret;
        }
    };
    

Log in to reply
 

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