算法训练营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;
    }
};
相关推荐
wyhwust6 分钟前
交换排序法&冒泡排序法& 选择排序法&插入排序的算法步骤
数据结构·算法·排序算法
利刃大大9 分钟前
【动态规划:背包问题】完全平方数
c++·算法·动态规划·背包问题·完全背包
wyhwust38 分钟前
数组----插入一个数到有序数列中
java·数据结构·算法
im_AMBER42 分钟前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
gihigo19981 小时前
基于MATLAB的IEEE 14节点系统牛顿-拉夫逊潮流算法实现
开发语言·算法·matlab
万物挽挽1 小时前
数据结构核心
数据结构
甄心爱学习2 小时前
数据挖掘-聚类方法
人工智能·算法·机器学习
ID_180079054732 小时前
基于 Python 的淘宝商品详情数据结构化解析:SKU、价格与库存字段提取
开发语言·数据结构·python
星释2 小时前
Rust 练习册 82:Hamming与字符串处理
开发语言·算法·rust
小张成长计划..3 小时前
【C++】16:模板进阶
c++·算法