代码随想录算法训练营第三十五天 |LC 406. 根据身高重建队列、LC 452. 用最少数量的箭引爆气球

406. 根据身高重建队列

思路

先按照身高从大到小排列, 然后再按顺序把前面有多少个插入,因为最前面一定是高的,所以不影响后面排布顺序

代码

class Solution {
static bool cmp (vector<int>& a, vector<int>& b){
    if (a[0] == b[0]) return a[1] < b[1];
    else return a[0] > b[0];
}
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(), cmp);
        list<vector<int>> res;
        for (int i = 0;i < people.size(); i ++){
            auto it = res.begin();
            for (int j = 0; j < people[i][1]; j ++) it ++;
            res.insert(it, people[i]);
        }
        return vector<vector<int>> (res.begin(), res.end());
    }
};

452. 用最少数量的箭引爆气球

思路

看重合区间,如果本次和之前有重合,去尾端最小值,如果没重合就是重新开始一轮

代码

class Solution {
static bool cmp(vector<int>& a, vector<int>& b){
    if (a[0] == b[0]) return a[1] < b[1];
    return a[0] < b[0];
}
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        int res = 1;
        sort(points.begin(), points.end(), cmp);
        int end = points[0][1];
        // cout << points[0][1] << endl;
        for (int i = 0; i < points.size(); i ++){
            // cout << end << endl;
            if (end >= points[i][0]){
                end = min (end, points[i][1]);
                continue;
            }
            else {
                res ++;
                end = points[i][1];
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值