406. Queue Reconstruction by Height

这道题也比较基础,算是贪心吧?每一步把当前最大最后的归位就好。

自己的解

class Solution {
public:
    vector> reconstructQueue(vector>& people) {
        int now = people.size();
        while (now != 0){
            int biggest = 0;
            for (int i = 0; i < now; i++){
                if (people[i].first > people[biggest].first){
                    biggest = i;
                }else if (people[i].first == people[biggest].first && people[i].second < people[biggest].second){
                    biggest = i;
                }
            }
            --now;
            swap(people[biggest], people[now]);
            pair  temp = people[now];
            for(int i = now; i 

别人家的解。。。

vector> reconstructQueue(vector>& people) {
    auto comp = [](const pair& p1, const pair& p2)
                    { return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second); };
    sort(people.begin(), people.end(), comp);
    vector> res;
    for (auto& p : people) 
        res.insert(res.begin() + p.second, p);
    return res;
}

总结,多熟悉库函数T T

你可能感兴趣的:(406. Queue Reconstruction by Height)