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

LeetCode 860.柠檬水找零

        在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。

        注意,一开始你手头没有任何零钱。

        如果你能给每位顾客正确找零,返回 true ,否则返回 false 。

视频讲解icon-default.png?t=N0U7https://www.bilibili.com/video/BV12x4y1j7DD/?spm_id_from=333.788&vd_source=f98f2942b3c4cafea8907a325fc56a48文章讲解https://programmercarl.com/0860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6.html#%E6%80%9D%E8%B7%AF

  • 思路:维护两种金额的数量:5、10(20不能用于找零,不用维护)
    • 情况一:账单是5,直接收下。
    • 情况二:账单是10,消耗一个5,增加一个10
    • 情况三:账单是20,优先消耗一个10和一个5,如果不够,再消耗三个5
      • 局部最优:遇到账单20,优先消耗美元10,完成本次找零
      • 全局最优:完成全部账单的找零
  • 代码:
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            // 情况一
            if (bill == 5) five++;
            // 情况二
            if (bill == 10) {
                if (five <= 0) return false;
                ten++;
                five--;
            }
            // 情况三
            if (bill == 20) {
                // 优先消耗10美元,因为5美元的找零用处更大,能多留着就多留着
                if (five > 0 && ten > 0) {
                    five--;
                    ten--;
                } else if (five >= 3) {
                    five -= 3;
                } else return false;
            }
        }
        return true;
    }
};

LeetCode 406.根据身高重建队列

        假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。

        请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队首的人)。

视频讲解文章讲解https://programmercarl.com/0406.%E6%A0%B9%E6%8D%AE%E8%BA%AB%E9%AB%98%E9%87%8D%E5%BB%BA%E9%98%9F%E5%88%97.html

  • 思路:本题有 h 和 k 两个维度,可以先按一个维度排序
    • 方法一:按身高从大到小排序,再以 k 为下标重新插入队列406.根据身高重建队列 

                                    [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

      排序完的people: [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]

      插入的过程:

              插入[7,0]:[[7,0]]

              插入[7,1]:[[7,0], [7,1]]

              插入[6,1]:[[7,0], [6,1], [7,1]]

              插入[5,0]:[[5,0], [7,0], [6,1], [7,1]]

              插入[5,2]:[[5,0], [7,0], [5,2], [6,1], [7,1]]

              插入[4,4]:[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

    • 方法二:按 k 从小到大排序,再通过比较身高寻找插入位置

                                    [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

      排序完的people: [[7,0], [5,0], [7,1], [6,1], [5,2], [4,4]]

      插入的过程:

              插入[7,0]:[[7,0]]

              插入[5,0]:[[5,0], [7,0]]

              插入[7,1]:[[5,0], [7,0], [7,1]]

              插入[6,1]:[[5,0], [7,0], [6,1], [7,1]]

              插入[5,2]:[[5,0], [7,0], [5,2], [6,1], [7,1]]

              插入[4,4]:[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

  • 代码:
// 方法一:
// 版本一:时间复杂度:O(nlog n + n^2)    空间复杂度:O(n)
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];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort (people.begin(), people.end(), cmp);
        vector<vector<int>> que;
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1];
            que.insert(que.begin() + position, people[i]);
        }
        return que;
    }
};

// 版本二:时间复杂度:O(nlog n + n^2)    空间复杂度:O(n)
class Solution {
public:
    // 身高从大到小排(身高相同k小的站前面)
    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];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort (people.begin(), people.end(), cmp);
        list<vector<int>> que; // list底层是链表实现,插入效率比vector高的多
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1]; // 插入到下标为position的位置
            std::list<vector<int>>::iterator it = que.begin();
            while (position--) { // 寻找在插入位置
                it++;
            }
            que.insert(it, people[i]);
        }
        return vector<vector<int>>(que.begin(), que.end());
    }
};

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

        在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了。开始坐标总是<=结束坐标。

        一支弓箭可以沿着 x 轴从不同点完全垂直地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足  xstart ≤ x ≤ xend,则该气球会被引爆。可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。

        给你一个数组 points ,其中 points [i] = [xstart,xend] ,返回引爆所有气球所必须射出的最小弓箭数。

4个气球,最少需2支箭
4个气球,最少需2支箭

视频讲解
文章讲解https://programmercarl.com/0452.%E7%94%A8%E6%9C%80%E5%B0%91%E6%95%B0%E9%87%8F%E7%9A%84%E7%AE%AD%E5%BC%95%E7%88%86%E6%B0%94%E7%90%83.html

⭐重叠区间

  • 思路:
    • 局部最优:当气球出现重叠,一起射,所用弓箭最少;
    • 全局最优:把所有气球射爆所用弓箭最少。
    • 按照起始位置排序,从前向后遍历气球数组👉只要当前 points[i][0] > right,说明不重叠
    • 不重叠:箭数+1
    • 重叠:更新重叠区间右边界right
  • 代码:
class Solution {
private:
    static bool cmp(const vector<int>& a, const vector<int>& b) {
        return a[0] < b[0];
    }
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.size() == 0) return 0;
        sort(points.begin(), points.end(), cmp);    // 按气球起始位置从小到大排序
        int right = points[0][1];    // 初始化重叠区间右边界
        int result = 1; // points 不为空至少需要一支箭
        for (int i = 1; i < points.size(); ++i) {
            if (points[i][0] > right) {  // 不重叠
                ++result; // 需要一支箭
                right = points[i][1];    // 更新重叠区间
            }
            else {  // 重叠,缩小重叠区间
                right = min(right, points[i][1]);
            }
        }
        return result;
    }
};
// 代码随想录:不维护变量right,在需要缩小重叠区间时直接修改points[i][1]
class Solution {
private:
    static bool cmp(const vector<int>& a, const vector<int>& b) {
        return a[0] < b[0];
    }
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.size() == 0) return 0;
        sort(points.begin(), points.end(), cmp);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值