Short solution with sort() and insert() c++

  • 0
        typedef pair<int,int> t;
        vector<t> reconstructQueue(vector<t>& people) {
            sort(people.begin(), people.end(),[](t &left, t & right) {
                return (left.first == right.first)? 
                        left.second <= right.second : left.first >= right.first;});
            vector<t> res;
            for(int i = 0; i < people.size(); ++i)
                res.insert(res.begin()+people[i].second, people[i]);
            return res;

Log in to reply

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