贪心算法part04 算法

贪心算法part04 算法

● 860.柠檬水找零

● 406.根据身高重建队列

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

1.leetcode 860.柠檬水找零

https://leetcode.cn/problems/lemonade-change/description/

java 复制代码
class Solution {
    public boolean lemonadeChange(int[] bills) {
        //看能不能找零
        //bills[i] 不是 5 就是 10 或是 20 ,已经固定好了
        //遇见5,我们就直接收起来
        //遇见10我们就找张5块的给他,10元收起来
        //遇见20我们就两种找零方式,优先10+5,再5+5+5
        
        //计每种面额的数量
        int five=0;
        int ten=0;
        int twenty=0;
        for(int i=0;i<bills.length;i++){
            if(bills[i]==5){
                five++;
            }else if(bills[i]==10){
                if(five>0){
                    //有钱可以找
                    five--;
                    ten++;
                }else{
                    //没钱可以找零
                    return false;
                }
            }else if(bills[i]==20){
                if(ten>0&&five>0){
                    //可以找
                    ten--;
                    five--;
                    twenty++;
                }else if(five>=3){
                    //三张五块的,也可以找
                    five=five-3;
                    twenty++;
                }else{
                    //没得找零
                    return false;
                }
            }
        }
        //上面都没有返回false
        //那就是都能被找零
        return true;
    }
}

2.leetcode 406.根据身高重建队列

https://leetcode.cn/problems/queue-reconstruction-by-height/description/

java 复制代码
class Solution {
    public int[][] reconstructQueue(int[][] people) {
        //身高从大到小排,(身高相同的k小的站i前面)
        Arrays.sort(people,(a,b)->{
            if(a[0]==b[0]){
                //身高相同
                return a[1]-b[1];
            }
            return b[0]-a[0];//b-a是降序排列
        });
        //定义一个存放结果的变量
        LinkedList<int[]> result=new LinkedList<>();
        for(int i=0;i<people.length;i++){
            //将k有值的,不为0的插到对应的位置
            //记录要插入的位置
            int pesition=people[i][1];//也就是k
            result.add(pesition,people[i]);//将值插到指定index去  index,value
        }
        return result.toArray(new int[people.length][]);
    }
}

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

https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/

java 复制代码
class Solution {
    public int findMinArrowShots(int[][] points) {
        //画图模拟过程才好容易理解
        //如果都没气球,那么都不用射箭
        if(points.length==0){return 0;}
        //对气球的左边界进行排序
        Arrays.sort(points,(a,b)->Integer.compare(a[0],b[0]));
        //point气球不为空至少需要一只箭
        int count=1;
        for(int i=1;i<points.length;i++){
            //i从1开始
            //如果现在的气球左区间大于上一个气球的右区间
            if(points[i][0]>points[i-1][1]){
                //证明没有重叠
                count++;
            }else{
                //气球重叠了,那就更新右区间
                points[i][1]=Math.min(points[i][1],points[i-1][1]);
            }
        }
        return count;
    }
}
相关推荐
Dingdangcat861 小时前
城市交通多目标检测系统:YOLO11-MAN-FasterCGLU算法优化与实战应用_3
算法·目标检测·目标跟踪
tang&2 小时前
滑动窗口:双指针的优雅舞步,征服连续区间问题的利器
数据结构·算法·哈希算法·滑动窗口
拼命鼠鼠2 小时前
【算法】矩阵链乘法的动态规划算法
算法·矩阵·动态规划
LYFlied2 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
式5162 小时前
线性代数(八)非齐次方程组的解的结构
线性代数·算法·机器学习
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——翻转对
算法·排序算法·结构与算法
叠叠乐3 小时前
robot_state_publisher 参数
java·前端·算法
hweiyu004 小时前
排序算法:冒泡排序
算法·排序算法
brave and determined4 小时前
CANN训练营 学习(day9)昇腾AscendC算子开发实战:从零到性能冠军
人工智能·算法·机器学习·ai·开发环境·算子开发·昇腾ai
Dave.B4 小时前
用【vtk3DLinearGridCrinkleExtractor】快速提取3D网格相交面
算法·3d·vtk