【代码随想录】刷题笔记Day37

前言

  • 试一试早上+晚上固定时间刷题会不会效率and养成习惯

135. 分发糖果 - 力扣(LeetCode)

  • 两边一起判断容易顾此失彼
  • 从左到右遍历,只比较右比左大的情况,局部and全局:右比左大
  • 从右到左遍历,只比较左比右大的情况,局部and全局:左比右大
  • 取两次遍历得到的最大值,局部and全局:比左右都大
cpp 复制代码
class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candyVec(ratings.size(), 1);
        // 从前向后
        for (int i = 1; i < ratings.size(); i++) {
            if (ratings[i] > ratings[i - 1]) candyVec[i] = candyVec[i - 1] + 1;
        }
        // 从后向前
        for (int i = ratings.size() - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1] ) {
                candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1);
            }
            // 也可以用更新的策略
            // if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
            //     candies[i] = candies[i + 1] + 1;
            // }
        }
        // 统计结果
        int result = 0;
        for (int i = 0; i < candyVec.size(); i++) result += candyVec[i];
        return result;
    }
};

860. 柠檬水找零 - 力扣(LeetCode)

  • 这题模拟就行,唯一需要贪心的就是20的时候优先找10+5,而不是5+5+5
cpp 复制代码
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++;
            }else if(bills[i] == 10){
                if(five <= 0) return false;
                ten++;
                five--;
            }else if(bills[i] == 20){  // 实际不用维护twenty的数,找不出去
                if(ten >= 1 && five >= 1){  // 优先消耗5+10
                    ten--;five--;
                } else if(ten <= 0 && five >= 3) five-= 3;
                else return false; 
            }
        }
        return true;
    }
};

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

  • 双端遍历,要领是第二遍不影响第一遍确定好的符合题目意思的排序(类似分发糖果)
  • 先按照h从大到小排(相同则k从小到大),再根据k的值进行插入(不影响其他符合题意)
cpp 复制代码
// 用vector效率低,链表效率高
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) {
        vector<vector<int>> reQueue;
        // list<vector<int>> que; // list底层是链表实现,插入效率比vector高的多
        sort(people.begin(), people.end(), cmp);
        for(int i = 0; i < people.size(); i++){
            int pos = people[i][1];
            reQueue.insert(reQueue.begin() + pos, people[i]);
            // 链表插入比容器数组效率高,但是没有随机访问功能,只能遍历找插入位置
            // std::list<vector<int>>::iterator it = reQueue.begin();
            // while (pos--) { // 寻找插入位置
            //     it++;
            // }
            // reQueue.insert(it, people[i]);
        }
        return reQueue;
        // return vector<vector<int>>(reQueue.begin(), reQueue.end());
    }
};

后言

  • 还是折腾到下午了,感觉早上的2小时还是不太够啊,来不及呀来不及~
相关推荐
luckys.one1 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
~|Bernard|2 小时前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师2 小时前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo32 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
汇能感知3 小时前
摄像头模块在运动相机中的特殊应用
经验分享·笔记·科技
阿巴Jun3 小时前
【数学】线性代数知识点总结
笔记·线性代数·矩阵
好家伙VCC3 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
茯苓gao4 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
是誰萆微了承諾4 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang