代码随想录算法训练营Day29 | 134. 加油站 | 135. 分发糖果 | 860.柠檬水找零 | 406.根据身高重建队列

今日任务

134. 加油站

  • 题目链接: https://leetcode.cn/problems/gas-station/
  • 题目描述
    在这里插入图片描述

Code

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        // int current_gas, current_cost;
        // int len, j;
        // if(gas.size()==1&&gas[0] >= cost[0]) return 0;
        // for (int i = 0; i < gas.size(); i++) {
        //     if (gas[i] <= cost[i]) continue;
        //     current_gas = gas[i];
        //     len = gas.size();
        //     j = i;
        //     while (--len) {
        //         current_cost = cost[j];
        //         if (++j >= gas.size()) j = 0;
        //         if (current_gas >= current_cost) {
        //             current_gas = current_gas - current_cost + gas[j];
        //         }
        //         else {
        //             break;
        //         }
        //     }
        //     if (len == 0 && current_gas - cost[j] >= 0) return i;
        // }
        // return -1;

        // int sum=0,judge_pos=0,n=0;
        // for(int i=0;i<gas.size();i++){
        //     sum+=gas[i]-cost[i];
        //     judge_pos+=gas[i]-cost[i];
        //     if(judge_pos<0){
        //         n=i+1;
        //         judge_pos=0;
        //     }
        // }
        // if(sum<0) return -1;
        // return n;

        // int n = gas.size();
        // vector<int> diff(n);
        // for(int i = 0; i < n; i++){
        //     diff[i] = gas[i] - cost[i];
        // }
        // int post = 0;
        // int sum = reduce(diff.begin(), diff.end(), 0);
        // if(sum < 0){
        //     return -1;
        // }
        // int ans = 0;
        // for(int i = n - 1; i >=0 ; i--){
        //     post += diff[i];
        //     if(post >= 0 && diff[i] >= 0){
        //         ans = i;
        //     }else if(post - diff[i] >= 0 && diff[i] < 0){
        //         post = diff[i];
        //     }
        // }
        // return ans;

        int sum = 0;
        int pre = 0;
        int n = gas.size();
        int ans = 0;
        for(int i = 0; i < n; i++){
            int diff = gas[i] - cost[i];
            sum += diff;
            pre += diff;
            if(pre < 0){
                pre = 0;
                ans = i + 1;
            }
        }
        return sum < 0 ? -1 : ans;
    }
};

135. 分发糖果

  • 题目链接: https://leetcode.cn/problems/candy/description/
  • 题目描述
    在这里插入图片描述

Code 遍历两次

class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size();
        vector<int> candy(n, 1);
        for(int i = 1; i < n; i++){
            if(ratings[i] > ratings[i - 1]){
                candy[i] = candy[i - 1] + 1;
            }
        }
        for(int i = n - 2; i >= 0; i--){
            if(ratings[i] > ratings[i + 1]){
                candy[i] = candy[i] > candy[i + 1] ? candy[i] : candy[i + 1] + 1;
            }
        }
        return reduce(candy.begin(), candy.end(), 0);
    }
};

860.柠檬水找零

  • 题目链接: https://leetcode.cn/problems/lemonade-change/description/
  • 题目描述
    在这里插入图片描述

Code

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        // if(bills[0] != 5){
        //     return false;
        // }
        // unordered_map<int, int> money;
        // int n = bills.size();
        // for(int i = 0; i < n; i++){
        //     int pay = bills[i];

        //     if(pay == 10){
        //         if(money[5]){
        //             money[5]--;
        //         }else{
        //             return false;
        //         }
        //     }
        //     if(pay == 20){
        //         if(money[10] && money[5]){
        //             money[10]--;
        //             money[5]--;
        //         }else if(money[5] >= 3){
        //             money[5] -= 3;
        //         }else{
        //             return false;
        //         }

        //     }

        //     money[pay]++;
        // }
        // return true;

        int five = 0, ten = 0;
        for(int b : bills){
            if(b == 5){
                five++;
            }else if(b == 10){
                five--;
                ten++;
            }else if(ten){
                ten--;
                five--;
            }else{
                five -= 3;
            }
            if(five < 0){
                return false;
            }
        }
        return true;
    }
};

406.根据身高重建队列

  • 题目链接: https://leetcode.cn/problems/queue-reconstruction-by-height/description/
  • 题目描述
    在这里插入图片描述

Code 排序+倒序插入

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        auto cmp = [&](auto &a, auto &b)->bool{
            return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0];
        };
        sort(people.begin(), people.end(), cmp);

        list<vector<int>> ans;
        for(auto &elem : people){
            ans.insert(next(ans.begin(), elem[1]), elem);
        }
        return vector<vector<int>>(ans.begin(), ans.end());
    }
};
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值