WEEK#2 Queue Reconstruction by Height

WEEK#2 Queue Reconstruction by Height_第1张图片
Problem Description
  1. Sort the queue by height(h), if height equal, sort by k, desendingly
  2. Insert all elements into the result queue by k
bool mycompare(pair p1, pairp2) {
    return (p1.first > p2.first) || (p1.first == p2.first && p1.second < p2.second);
}

class Solution {
public:    
    vector> reconstructQueue(vector>& people) {
        vector> Result;
        //cout << people.size();
        Result.resize(people.size());
        sort(people.begin(), people.end(), mycompare);
        for (auto person : people) {
            int count = person.second;
            //cout << count << endl;
            auto it = Result.begin();
            while (count--)
                it++;
            Result.insert(it, person);
        }
        Result.resize(people.size());
        return Result;
    }
};

你可能感兴趣的:(WEEK#2 Queue Reconstruction by Height)