代码随想录day29 贪心03

134. 加油站

cpp 复制代码
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int cursum = 0;
        int totalsum = 0;
        int start = 0;
        for (int i = 0; i < gas.size(); i++) {
            cursum += gas[i] - cost[i];
            totalsum += gas[i] - cost[i];
            if (cursum < 0) {
                start = i + 1;
                cursum = 0;
            }
        }
        if (totalsum < 0)
            return -1;
        return start;
    }
};

135. 分发糖果

cpp 复制代码
class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candys(ratings.size(), 1);
        for (int i = 1; i < ratings.size(); i++) {
            if (ratings[i - 1] < ratings[i]) {
                candys[i] = candys[i - 1] + 1;
            }
        }
        for (int i = ratings.size() - 1; i > 0; i--) {
            if (ratings[i] < ratings[i - 1]) {
                candys[i - 1] = max(candys[i] + 1, candys[i - 1]);
            }
        }
        int sum = 0;
        for (auto a : candys) {
            sum += a;
        }
        return sum;
    }
};

860. 柠檬水找零

cpp 复制代码
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int bill5 = 0;
        int bill10 = 0;
        for (auto a : bills) {
            if (a == 5) {
                bill5++;
            } else if (a == 10) {
                if (bill5 <= 0)
                    return false;
                bill10++;
                bill5--;
            } else {
                if (bill10 > 0 && bill5 > 0) {
                    bill10--;
                    bill5--;
                } else if (bill5 >= 3) {
                    bill5 -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
};

406. 根据身高重建队列

cpp 复制代码
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) {
        sort(people.begin(), people.end(), cmp);
        vector<vector<int>> que;
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1];
            que.insert(que.begin() + position, people[i]);
        }
        return que;
    }
};

其实这道题主要是思路,大部分人无法想到按身高降序排序后,第二个参数便是在当前队列里要插入的位置。

相关推荐
Hello.Reader6 分钟前
算法基础(二)——算法为什么是一种核心技术
算法
rit84324996 分钟前
电容层析成像(ECT)的ART算法MATLAB演示实例
开发语言·算法·matlab
故事和你917 分钟前
洛谷-算法2-4-字符串2
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
cpp_25017 分钟前
P3374 【模板】树状数组 1
数据结构·c++·算法·题解·洛谷·树状数组
郝学胜-神的一滴8 分钟前
干货版《算法导论》 02 :算法效率核心解密
java·开发语言·数据结构·c++·python·算法
stolentime8 分钟前
AT_agc061_d [AGC061D] Almost Multiplication Table题解
c++·算法·构造
WL_Aurora9 分钟前
Python 算法基础篇之回溯
python·算法
智者知已应修善业10 分钟前
【51单片机控制的交通信号灯三按键切换调节时分秒加减】2023-8-26
c++·经验分享·笔记·算法·51单片机
MicroTech202511 分钟前
量子退火赋能:微算法科技(NASDAQ: MLGO)图像分割算法开启未来科技新视界
科技·算法·量子计算
枕星而眠12 分钟前
C语言数组专题:从一维到二维,吃透内存与指针
java·数据结构·算法