代码随想录算法训练营第二十九天|LeetCode134. 加油站、LeetCode135. 分发糖果、LeetCode860.柠檬水找零、LeetCode406.根据身高重建队列

目录

前言

LeetCode134. 加油站

LeetCode135. 分发糖果

LeetCode860.柠檬水找零

LeetCode406.根据身高重建队列

一、LeetCode134. 加油站

题目链接:

代码:

二、LeetCode135. 分发糖果

 题目链接:

代码:

三、LeetCode860.柠檬水找零

 题目链接:

代码:

四、LeetCode406.根据身高重建队列

 题目链接:

代码:


前言

LeetCode134. 加油站

文章讲解:代码随想录

视频讲解:LeetCode :134.加油站

LeetCode135. 分发糖果

文章讲解:代码随想录

视频讲解:LeetCode:135.分发糖果

LeetCode860.柠檬水找零

文章讲解:代码随想录

视频讲解:LeetCode:860.柠檬水找零

LeetCode406.根据身高重建队列

文章讲解:代码随想录

视频讲解:LeetCode:406.根据身高重建队列


一、LeetCode134. 加油站

题目链接:

134. 加油站 - 力扣(LeetCode)

代码:

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;
    }
};

二、LeetCode135. 分发糖果

 题目链接:

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

代码:

class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candy(ratings.size(), 1);
        for(int i = 1; i < candy.size(); i++)
        {
            if(ratings[i] > ratings[i - 1])
            {
                candy[i] = candy[i - 1] + 1;
            }
        }

        for(int i = candy.size() - 2; i >= 0; i--)
        {
            if(ratings[i] > ratings[i + 1])
            {
                candy[i] = max(candy[i], candy[i + 1] + 1);
            }
        }
        int sum = 0;
        for(int num:candy)
        {
            sum += num;
        }
        return sum;
    }
};

三、LeetCode860.柠檬水找零

 题目链接:

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

代码:

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five=0,ten=0;
        for(auto&bill:bills){
            if(bill==5){
                five++;
            }
            else if(bill==10){
                ten++;
                five--;
            }
            else{
                if(five>0&&ten>0){
                    five--;
                    ten--;
                }
                else{
                    five-=3;
                }
            }
            if(five<0||ten<0){
                return false;
            }
        }
        return true;
    }
};

四、LeetCode406.根据身高重建队列

 题目链接:

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

代码:

bool compare(vector<int>& left, vector<int>& right)
{
    if(left[0] > right[0])
    {
        return true;
    }
    else if(left[0] == right[0] && left[1] <= right[1])
    {
        return true;
    }
    return false;
}

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(), compare);
        vector<vector<int>> queue;
        for(int i = 0; i < people.size(); i++)
        {
            int pos = people[i][1];
            queue.insert(queue.begin() + pos, people[i]);
        }
        return queue;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值