WEEK#2 Queue Reconstruction by Height

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<int,int> p1, pair<int,int>p2) {
    return (p1.first > p2.first) || (p1.first == p2.first && p1.second < p2.second);
}

class Solution {
public:    
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        vector<pair<int,int>> 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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容