leetcode 406. 根据身高重建队列

https://leetcode-cn.com/problems/queue-reconstruction-by-height/submissions/

    [c++] 排序,然后插入。

    假设候选队列为 A,已经站好队的队列为 B.

    从 A 里挑身高最高的人 x 出来,插入到 B. 因为 B 中每个人的身高都比 x 要高,因此 x 插入的位置,就是看 x 前面应该有多少人就行了。比如 x 前面有 5 个人,那 x 就插入到队列 B 的第 5 个位置。

class Solution {
public:
    vector> reconstructQueue(vector>& people) {
        // 先排序
        // [7,0], [7,1], [6,1], [5,0], [5,2], [4,4]
        
        // 再一个一个插入。
        // [7,0]
        // [7,0], [7,1]
        // [7,0], [6,1], [7,1]
        // [5,0], [7,0], [6,1], [7,1]
        // [5,0], [7,0], [5,2], [6,1], [7,1]
        // [5,0], [7,0], [5,2], [6,1], [4,4], [7,1]
        sort(people.begin(), people.end(), [](const vector& a, const vector& b) {
            if (a[0] > b[0]) return true;
            if (a[0] == b[0] && a[1] < b[1]) return true;
            return false;
        });
        
        vector> res;
        for (auto& e : people) {
            res.insert(res.begin() + e[1], e);
        }
        return res;     
    }
};

 

你可能感兴趣的:(leetcode)