算法训练营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;
    }
};
相关推荐
体系结构论文研讨会27 分钟前
多项式环及Rq的含义
算法
智驱力人工智能36 分钟前
极端高温下的智慧出行:危险检测与救援
人工智能·算法·安全·行为识别·智能巡航·高温预警·高温监测
森焱森44 分钟前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源
课堂剪切板2 小时前
ch07 题解
算法·深度优先
R_AirMan2 小时前
深入浅出Redis:一文掌握Redis底层数据结构与实现原理
java·数据结构·数据库·redis
科大饭桶3 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
小高Baby@3 小时前
map数据结构在Golang中是无序的,并且键值对的查找效率较高的原因
数据结构
北风toto3 小时前
python学习DataFrame数据结构
数据结构·python·学习
我爱C编程5 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7895 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法