算法学习day35贪心part04-860、406、452

发布时间 2023-05-31 19:47:53作者: 坤坤无敌
package LeetCode.greedypart04;
/**
* 860. 柠檬水找零
* 在柠檬水摊上,每一杯柠檬水的售价为 5美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。
* 每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。
* 注意,一开始你手头没有任何零钱。
* 给你一个整数数组 bills ,其中 bills[i] 是第 i 位顾客付的账。如果你能给每位顾客正确找零,返回true,否则返回 false。
* 示例:
* 输入:bills = [5,5,5,10,20]
* 输出:true
* */

public class LemonadeChange_860 {
    public static void main(String[] args) {
        int [] bills = {5,5,5,10,20};
        boolean flag = lemonadeChange(bills);
        System.out.println(flag);

    }
    public static boolean lemonadeChange(int[] bills) {
        int five = 0;
        int ten = 0;

        for (int i = 0; i < bills.length; i++) {
            if (bills[i] == 5) {
                five++;
            } else if (bills[i] == 10) {
                five--;
                ten++;
            } else if (bills[i] == 20) {
                if (ten > 0) {
                    ten--;
                    five--;
                } else {
                    five -= 3;
                }
            }
            if (five < 0 || ten < 0) return false;
        }
        return true;
    }

}
package LeetCode.greedypart04;
/**
 * 406. 根据身高重建队列
 * 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。
 * 每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。
 * 请你重新构造并返回输入数组 people 所表示的队列。
 * 返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。
 * 示例:
 * 输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
 * 输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
 * */

import java.util.Arrays;
import java.util.LinkedList;

/**
 * 思路:
 * 遇到两个维度权衡的时候,一定要先确定一个维度,再确定另一个维度,如果两个维度一起考虑,一定会顾此失彼
 * 这道题目只能按照身高排序。前面的节点一定都比本节点高
 * */

public class QueueReconstructionByHeight_406 {
    public static void main(String[] args) {
        int [][] people = {{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}};
        int [][] result = reconstructQueue(people);
    }

    public static int[][] reconstructQueue(int[][] people) {
        // 身高从大到小排(身高相同k小的站前面)
        Arrays.sort(people, (a, b) -> {
            if (a[0] == b[0]) return a[1] - b[1];
            return b[0] - a[0];
        });

        LinkedList<int[]> que = new LinkedList<>();

        for (int[] p : people) {
            que.add(p[1],p);
        }

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

}
package LeetCode.greedypart04;
import java.util.Arrays;

/**
 * 452. 用最少数量的箭引爆气球
 * 有一些球形气球贴在一堵用 XY 平面表示的墙面上。
 * 墙面上的气球记录在整数数组points,其中points[i] = [xstart, xend]表示水平直径在xstart和xend之间的气球。你不知道气球的确切 y 坐标。
 * 一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。
 * 在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart≤ x ≤ xend,则该气球会被 引爆。
 * 可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。
 * 给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数。
 * 示例:
 * 输入:points = [[10,16],[2,8],[1,6],[7,12]]
 * 输出:2
 * 解释:气球可以用2支箭来爆破:
 * -在x = 6处射出箭,击破气球[2,8]和[1,6]。
 * -在x = 11处发射箭,击破气球[10,16]和[7,12]。
 * */

public class MinimumNumberOfArrowsToBurstBalloons_452 {
    public static void main(String[] args) {
        int[][] points = {{10,16},{2,8},{1,6},{7,12}};
        int result = findMinArrowShots(points);
        System.out.println(result);
    }
    public static int findMinArrowShots(int[][] points) {
        // 根据气球直径的开始坐标从小到大排序
        // 使用Integer内置比较方法,不会溢出
        Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));

        int count = 1;  // points 不为空至少需要一支箭
        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > points[i - 1][1]) {  // 气球i和气球i-1不挨着,注意这里不是>=
                count++; // 需要一支箭
            } else {  // 气球i和气球i-1挨着
                points[i][1] = Math.min(points[i][1], points[i - 1][1]); // 更新重叠气球最小右边界
            }
        }
        return count;
    }

}