LeetCode 860柠檬水找零 406根据身高重建队列 452用最少数量的箭引爆气球 | 代码随想录25期训练营day35

贪心算法4

LeetCode 860 柠檬水找零 2023.11.28

bool lemonadeChange(vector<int>& bills) {
    //用无序map键值对存储手中钱的金额及张数,钱包作用
    unordered_map<int, int> unmap;
    //开始遍历
    for (int i = 0; i < bills.size(); i++)
    {
        //如果付款为5r,则直接存到钱包里
        if(bills[i] == 5)
            unmap[5]++;
        //如果付款10r,则判断钱包里是否有面额5的钞票,有则5r减一张,10r加一张;没有的话返回false
        if(bills[i] == 10)
        {
            if(unmap[5] > 0)
            {
                unmap[5]--;
                unmap[10]++;
            }
            else
                return false;
        }
        //如果付款20r,则先判断钱包里是否有面额10r和面额5r的钞票,有则10r减一张,5r减一张;
        //没有的话判断5r的张数是否大于等于3,如果够,则5r张数减3;
        //都不够则返回false
        if(bills[i] == 20)
        {
            if(unmap[10] > 0 && unmap[5] > 0)
            {
                unmap[10]--;
                unmap[5]--;
            }
            else if(unmap[10] == 0 && unmap[5] >= 3)
                unmap[5] -= 3;
            else
                return false;
        }
    }
    return true;
}

LeetCode 406 根据身高重建队列 2023.11.28

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
    //先按身高从高到低排序,身高相同时,序号小的在前
    //冒泡排序
    for (int i = 0; i < people.size(); i++)
    {
        vector<int>& max = people[i];
        for (int j = i; j < people.size(); j++)
        {
            if(people[j][0] > max[0])
            {
                vector<int> temp = people[j];
                people[j] = max;
                max = temp;
            }
            else if(people[j][0] == max[0])
            {
                if(max[1] > people[j][1])
                {
                    vector<int> temp = people[j];
                    people[j] = max;
                    max = temp;
                }
            }
        }
    }
    //再按序号(前面有n个大于等于当前身高)进行插入,在begin()+position位置插入
    vector<vector<int>> que;
    for (int i = 0; i < people.size(); i++)
    {
        int postion = people[i][1];
        que.insert(que.begin() + postion, people[i]);
    }
    return que;
}

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

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 a[0] < b[0];
    }

    int findMinArrowShots(vector<vector<int>>& points) {
        //result存储解
        int result = 0;
        //区间排序
        sort(points.begin(), points.end(), cmp);
        //开始遍历区间大小
        for (int i = 0; i < points.size(); i++)
        {
            //temp存储i的增加量
            int temp = 1;
            //right存储当前遍历最小右区间
            int right = points[i][1];
            //当多个区间有重叠(前一个的右区间大于后一个的左区间)时,循环判断
            while(i+temp < points.size() && right >= points[i+temp][0])
            {
                //更新最小右区间
                right = min(right, points[i+temp][1]);
                //i的增加量递增
                temp++;
            }
            //更新下一支箭的i值
            if(temp != 1)
                i += (temp-1);
            //箭数增加
            result++;
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值