美文网首页
406. Queue Reconstruction by Hei

406. Queue Reconstruction by Hei

作者: 铭小狮子酱 | 来源:发表于2020-06-07 09:42 被阅读0次

解法:
1: 按高度从高到低,高度相同时按k的值从小到大排序
2:按位置插入

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(), [](vector<int>& p1, vector<int>& p2){
            return (p1[0] == p2[0])? p1[1] < p2[1] : p1[0] > p2[0];
        });
        // for(auto& p: people)
        //     cout<<"("<<p[0]<<","<<p[1]<<") ";
        // cout<<endl;
        vector<vector<int>> res;
        for(auto& p : people){
            res.insert(res.begin() + p[1], p);
        }
        return res;
    }
};

相关文章

网友评论

      本文标题:406. Queue Reconstruction by Hei

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