算法训练营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;
    }
};
相关推荐
kisshyshy9 小时前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
猿人谷16 小时前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
复杂网络17 小时前
Stable Diffusion 视觉大模型微调技术深度调研
算法
复杂网络17 小时前
基于 Stable Diffusion 架构的视觉大模型代表性工作与原理深度解析
算法
MrZhao40017 小时前
Agent Loop 如何用 Hook 扩展:权限、日志与工具拦截
算法
MrZhao40017 小时前
Agent 为什么需要 Skills:别把所有知识都塞进 system prompt
算法
JieE2122 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2123 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack203 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树4 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色