算法训练营day37

题目1:860. 柠檬水找零 - 力扣(LeetCode)

这道题思路确实不难,想明白了就是固定思路,五块就收,十块收了找五块,二十这里注意,可以找 10 + 5 也可以 找 5 + 5 + 5 ,如果能都能找那就返回true, 否则就是false ,我这里用的是数组记录,其实用int 类型计数就可以了,ti

复制代码
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        if(bills.size() == 1) {
            if(bills[0] != 5) return false;
            else return true;
        }
        vector<int> num5;
        vector<int> num10;
        for(int i = 0;i < bills.size();i++) {
            std::cout << "i = " << i << std::endl;
            if(bills[i] == 5) {
                num5.push_back(bills[i]);
            }else if(bills[i] == 10) {
                if(!num5.empty()) {
                    num5.pop_back();
                    num10.push_back(bills[i]);
                }else return false;
            }else {
                if(!num10.empty() && !num5.empty()) {
                    num10.pop_back();
                    num5.pop_back();
                }else if(num5.size() >=3) {
                    num5.pop_back();
                    num5.pop_back();
                    num5.pop_back();
                }else return false;  
            }
        }
        return true;
    }
};

题目2:406. 根据身高重建队列 - 力扣(LeetCode)

这里根据身高排序,有大到小,然后如果身高一样就 根据 k 由小到大,然后根据k进行插入相应的位置

复制代码
class Solution {
public:
    static bool cmp(vector<int>& a, 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>> qu;
        for(int i = 0;i < people.size();i++) {
            int position = people[i][1];
            qu.insert(qu.begin() + position, people[i]);
        }
        return qu;
    }
};

题目3:452. 用最少数量的箭引爆气球 - 力扣(LeetCode)

复制代码
class Solution {
public:
    static bool cmp(vector<int>& a, vector<int>& b) {
        if(a[0] == b[0]) return a[1] < b[1];
        return a[0] < b[0];
    }
    int findMinArrowShots(vector<vector<int>>& points) {
        sort(points.begin(), points.end(), cmp);
        if(points.size() == 1) return 1;
        int reslut = 1;
        for(int i = 1;i < points.size();i++) {
            if(points[i][0] > points[i - 1][1]) {
                reslut++;
            }else {
                points[i][1] = min(points[i][1], points[i - 1][1]);
            }
        }
        return reslut;
    }
};
相关推荐
历程里程碑4 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
你撅嘴真丑11 小时前
第九章-数字三角形
算法
uesowys11 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder11 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮11 小时前
AI 视觉连载1:像素
算法
智驱力人工智能12 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥12 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风13 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風13 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT0613 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法