代码随想录一刷day35

本文介绍了三道力扣(LeetCode)上的编程题目,包括860.柠檬水找零,使用动态规划管理不同面额的硬币找零;406.根据身高重建队列,通过排序和双指针重建队列顺序;以及452.用最少数量的箭引爆气球,通过排序和遍历找到最少的射击次数。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、力扣860. 柠檬水找零

class Solution {
    public boolean lemonadeChange(int[] bills) {
        // Arrays.sort(bills);
        int five = 0, ten = 0, twenty = 0;
        for(int i = 0; i < bills.length; i ++){
            if(bills[i] == 5){
                five ++;
            }
            if(bills[i] == 10){
                if(five <= 0){
                    return false;
                }
                five --; ten ++;
            }
            if(bills[i] == 20){
                if(five >= 1 && ten >= 1){
                    five --; ten --; twenty ++;
                }else if(five >= 3){
                    five -= 3; twenty ++;
                }else{
                    return false;
                }
            }
        }
        return true;
    }
}

二、力扣406. 根据身高重建队列

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        // 身高从大到小排(身高相同k小的站前面)
        Arrays.sort(people, (a, b) -> {
            if (a[0] == b[0]) return a[1] - b[1];   // a - b 是升序排列,故在a[0] == b[0]的狀況下,會根據k值升序排列
            return b[0] - a[0];   //b - a 是降序排列,在a[0] != b[0],的狀況會根據h值降序排列
        });
        LinkedList<int[]> que = new LinkedList<>();

        for (int[] p : people) {
            que.add(p[1],p);   //Linkedlist.add(index, value),會將value插入到指定index裡。
        }

        return que.toArray(new int[people.length][]);
    }
}

三、力扣452. 用最少数量的箭引爆气球

class Solution {
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
        int count = 1; 
        for(int i = 1; i < points.length; i ++){
            if(points[i][0] > points[i-1][1]){
                count ++;
            }else{
                points[i][1] = Math.min(points[i-1][1], points[i][1]);
            }
        }
        return count;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值