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

本文介绍了三个编程问题:使用C++解决柠檬水找零、根据身高重建队列的排序算法以及找到引爆气球所需的最少箭矢数。每个问题都涉及到数组操作、排序和条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •  860.柠檬水找零 

  • class Solution {
    public:
        bool lemonadeChange(vector<int>& bills) {
            int w = 0, s = 0, e = 0;
            for (int i = 0; i < bills.size(); i++) {
                if (bills[i] == 5) {
                    w++;
                } else if (bills[i] == 10) {
                    if (w > 0) {
                        s++;
                        w--;
                    } else
                        return 0;
                } else {
                    if (w > 0 && s > 0) {
                        s--;
                        w--;
                        e++;
                    } else if (w >= 3) {
                        w -= 3;
                    } else
                        return 0;
                }
            }
            return 1;
        }
    };

  •  406.根据身高重建队列 

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

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

  • class Solution {
    public:
    static bool cmp(vector<int>& a,vector<int>& b){
        return a[0]<b[0];
    }
        int findMinArrowShots(vector<vector<int>>& points) {
            sort(points.begin(),points.end(),cmp);
            int ans=1;
            int r=points[0][1];
            for(int i=1;i<points.size();i++){
               if(r<=points[i][1]){
                ans++;
                r=points[i][1];
               }
            }
            return ans;
        }
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值