406. Queue Reconstruction by Height

https://www.cnblogs.com/grandyang/p/5928417.html

https://www.cnblogs.com/liziran/p/6106534.html
个子高的位置排好后,再怎么对个子矮的排,都不会影响个子高的人的相对位置

贪心的思想

class Solution {
public:
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        sort(people.begin(),people.end(),cmp);
        for(int i = 0;i < people.size();i++){
            if(people[i].second != i){
                auto p = people[i];
                people.erase(people.begin() + i);
                people.insert(people.begin() + p.second,p);
            }
        }
        return people;
    }
    static bool cmp(pair<int,int> a,pair<int,int> b){
        if(a.first > b.first || (a.first == b.first && a.second < b.second))
            return true;
        else
            return false;
    }
};

 

posted @ 2019-03-08 23:15  有梦就要去实现他  阅读(86)  评论(0编辑  收藏  举报