美文网首页
WEEK#2 Queue Reconstruction by H

WEEK#2 Queue Reconstruction by H

作者: DarkKnightRedoc | 来源:发表于2017-12-26 14:21 被阅读0次
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;
    }
};

相关文章

网友评论

      本文标题:WEEK#2 Queue Reconstruction by H

      本文链接:https://www.haomeiwen.com/subject/nqtjgxtx.html