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

860.柠檬水找零

题目链接:https://leetcode.cn/problems/lemonade-change/
文档讲解:https://programmercarl.com/0860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6.html
视频讲解:https://www.bilibili.com/video/BV12x4y1j7DD

思路

  • 定义一个数组cash来记录目前5美元和10美元的张数,不用记录20美元,因为不会用20美元来找零。然后根据情况加减张数。
  • 本题的贪心在于拿到20美元时优先用一张10美元和一张5美元找零,因为5美元更万能。

代码

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

分析:时间复杂度:O(n),空间复杂度:O(1)。

406.根据身高重建队列

题目链接:https://leetcode.cn/problems/queue-reconstruction-by-height/
文档讲解:https://programmercarl.com/0406.%E6%A0%B9%E6%8D%AE%E8%BA%AB%E9%AB%98%E9%87%8D%E5%BB%BA%E9…
视频讲解:https://www.bilibili.com/video/BV1EA411675Y

思路

  • 先按照身高对数组降序排序,如果身高一样,按照k的升序排序。
  • 然后按照k作为下标依次插入queue数组。

代码

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (a, b) -> {
            if (a[0] == b[0]) return a[1] - b[1]; // 按照h来排序,如果h相等,k小的排前面,升序
            return b[0] - a[0]; // 如果h不相等,就是按照h来排序,h大的排前面,降序
        });
        List<int[]> queue = new LinkedList<>();
        for (int[] p : people) {
            queue.add(p[1], p); // 将p按照k的值插入queue对应下标的位置
        }
        return queue.toArray(new int[people.length][]);
    }
}

分析:时间复杂度:O(nlogn + n2),空间复杂度:O(n)。
Arrays.sort()的时间复杂度是O(nlogn)。在LinkedList中,插入操作的时间复杂度是 O(k),其中 k 是插入位置的索引。在最坏情况下,插入位置可能是链表的末端,所以插入单个人的复杂度为 O(n)。因为有 n 个人需要插入,这部分的总时间复杂度是 O(n2)。

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

题目链接:https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/
文档讲解:https://programmercarl.com/0452.%E7%94%A8%E6%9C%80%E5%B0%91%E6%95%B0%E9%87%8F%E7%9A%84%E7…
视频讲解:https://www.bilibili.com/video/BV1SA41167xe

思路

  • 贪心的思路是有重复的气球就一起射了。
  • 先按照左边界从小到大排序。
  • 如果后一个气球的左边界大于前一个气球的右边界,箭数加一。
if (points[i][0] > points[i - 1][1]) res++;
  • 如果后一个气球的左边界小于前一个气球的右边界,说明可以用同一只箭,但需要更新后一个气球的右边界,更新为两个气球中的最小值。
else points[i][1] = Math.min(points[i][1], points[i - 1][1]);

代码

class Solution {
    public int findMinArrowShots(int[][] points) {
        if (points.length == 0) return 0;
        int res = 1;
        Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0])); // 使用Integer内置比较方法,不会溢出
        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > points[i - 1][1]) res++;
            else points[i][1] = Math.min(points[i][1], points[i - 1][1]);
        }
        return res;
    }
}

分析:时间复杂度:O(nlogn),空间复杂度:O(1)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值