Queue Reconstruction by Height(C++根据身高重建队列)

459 篇文章 1 订阅

解题思路:

(1)先排序,将空位留给比当前人的身高 高的人

参考网址:https://leetcode.com/problems/queue-reconstruction-by-height/discuss/427157/Three-different-C%2B%2B-solutions.-from-O(n2)-to-O(nlogn).-faster-than-99

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        auto comp=[&](vector<int> &a,vector<int> &b) {
            if(a[0]<b[0]) return true;
            else if(a[0]>b[0]) return false;
            else if(a[1]>b[1]) return true;
            else return false;
        };
        
        sort(people.begin(),people.end(),comp);
        vector<vector<int>> vec(people.size(),vector<int>());
        
        for(int i=0;i<people.size();i++) {
            int index=0;
            for(int j=0;j<vec.size();j++) {
                if(vec[j].empty()) {
                    if(index==people[i][1]) {
                        vec[j]=people[i];
                        break;
                    }
                    index++;
                }
            }
        }
        return vec;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值