DAY35:贪心算法part4、860\406\452

Leetcode: 860 柠檬水找零

有如下三种情况:

情况一:账单是5,直接收下。

情况二:账单是10,消耗一个5,增加一个10

情况三:账单是20,优先消耗一个10和一个5,如果不够,再消耗三个5

唯一不确定的其实在情况三。

账单是20的情况,为什么要优先消耗一个10和一个5呢?

因为美元10只能给账单20找零,而美元5可以给账单10和账单20找零,美元5更万能!

所以局部最优:遇到账单20,优先消耗美元10,完成本次找零。全局最优:完成全部账单的找零。

复制代码
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0, ten = 0, twenty = 0;
        for(int i = 0; i < bills.size(); i++){
            if(bills[i] == 5) five++;//统计5元的数量
            if(bills[i] == 10){//统计5、10元的数量
                if(five <= 0) return false;
                ten++;
                five--;
            }
            if(bills[i] ==20){//20找0的先后顺序
                if(five <= 0) return false;
                if(ten > 0){
                    ten--;
                    five--;
                }
                else{
                    if(five <= 2) return false;
                    else five = five - 3;
                }
            }
        } 
        return true;

    }
};

Leetcode: 406 根据身高重建队列

当出现两个维度的数值进行贪心的时候,我们需要注意两个维度一定要有先后顺序,不然容易顾此失彼。这道题目先对身高进行从高到低的排序,然后再考虑第二维的前后顺序问题。

代码随想录

时间复杂度: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;
    }
};

使用链表来实现

复制代码
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 用最少数量的箭引爆气球

当气球出现重叠的时候,一起射,使用箭的数量最少。

如果把气球排序之后,从前到后遍历气球,被射过的气球跳过,记录一下箭的数量就可以了。

但是比较考验代码功底

复制代码
class Solution {
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;
    }
};
相关推荐
-dzk-3 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
风筝在晴天搁浅3 小时前
hot100 78.子集
java·算法
Jasmine_llq4 小时前
《P4587 [FJOI2016] 神秘数》
算法·倍增思想·稀疏表(st 表)·前缀和数组(解决静态区间和查询·st表核心实现高效预处理和查询·预处理优化(提前计算所需信息·快速io提升大规模数据读写效率
超级大只老咪4 小时前
快速进制转换
笔记·算法
m0_706653234 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你914 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
qq_423233904 小时前
C++与Python混合编程实战
开发语言·c++·算法
TracyCoder1235 小时前
LeetCode Hot100(19/100)——206. 反转链表
算法·leetcode
m0_715575345 小时前
分布式任务调度系统
开发语言·c++·算法