C++ solution


  • 1
    B

    sort input array with decreasing height and increasing count

    class Solution {
    public:
        vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
            vector<pair<int, int>>result;
            sort(people.begin(), people.end(), 
            [](pair<int, int>p1, pair<int, int>p2){return p1.first>p2.first || (p1.first==p2.first && p1.second<p2.second);});
            for(auto e : people)
            {
                bool add = true;
                int count = 0;
                for(int i = 0; i < result.size(); i++)
                {
                    if(result[i].first>=e.first) count++;
                    if(count>e.second)
                    {
                        result.insert(result.begin()+i, e);
                        add = false;
                        break;
                    }
                }
                if(add) result.push_back(e);
            }
            return result;
        }
    };

Log in to reply
 

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