day25 贪心算法 part03

问题1:加油站

题目:

134. 加油站 - 力扣(LeetCode)

思路:

局部最优解 即每次我加完油可以跑到下一个加油站吧 全局最优也是如此

代码:

java 复制代码
public class 加油站 {
    public static void main(String[] args) {
        System.out.println(canCompleteCircuit(new int[]{1,2,3,4,5},new int[]{1,2,3,4,5}));
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int curSum = 0;
        int totalSum = 0;
        int index = 0;
        for (int i = 0; i < gas.length; i++) {
            curSum += gas[i] - cost[i];
            totalSum += gas[i] - cost[i];
            if (curSum < 0) {
                index = (i + 1) % gas.length ;
                curSum = 0;
            }
        }
        if (totalSum < 0) return -1;
        return index;
    }
}
问题2:分发糖果

题目:

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

思路:

两段分开看

代码:

java 复制代码
import java.lang.reflect.Array;
import java.util.Arrays;

public class 分发糖果 {
    public static void main(String[] args) {
        System.out.println(candy(new int[]{1,2,2}));
    }
    public static int candy(int[] ratings) {
        /*
        先遍历左边 再遍历右边
         */
        int [] candy = new int[ratings.length];
        Arrays.fill(candy, 1);
        for (int i = 0; i < ratings.length-1; i++){
            if (ratings[i+1] > ratings[i]){
                candy[i+1] =  (candy[i]+1);
            }
        }
        for (int i = ratings.length-1; i > 0; i--){
            if (ratings[i-1] > ratings[i] ){
                if(candy[i-1] <= candy[i]){
                    candy[i-1] =  (candy[i]+1);
                }
            }
        }
        int sum = 0;
        for (int num : candy){
            sum += num;
        }
        return sum;
    }




}
问题3:柠檬水找零

题目:

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

思路:

分情况讨论 计数的方法

代码:

java 复制代码
import java.util.Arrays;
import java.util.HashMap;

public class 柠檬水找零 {
    public static void main(String[] args) {
        System.out.println(lemonadeChange(new int[]{5,5,5,10,5,5,10,20,20,20}));
    }


    public static boolean lemonadeChange(int[] bills) {
        /*
        局部最优: 每次手上的钱可以找给下一个用户
         */
        int cun5 = 0;
        int cun10 = 0;
        if (bills[0] != 5) return false;
        for (int i = 0; i < bills.length; i++){
            if (bills[i] == 5) cun5 += 1;
            else if (bills[i] == 10){
                if (cun5 > 0){
                    cun5  -= 1;
                    cun10 += 1;
                }else return false;
            }
            else if(bills[i] == 20){
                if ((cun5 >=1 && cun10 >=1)){
                    cun5 -= 1;
                    cun10-= 1;
                }else if (cun10 == 0 && cun5 >= 3){
                        cun5 -= 3;
                    }
                else return false;
            }
        }
        return true;
    }

}
问题4:根据身高重建队列

题目:

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

思路:

也是需要分两个维度去看 一个是身高维度 一个是多少人比他高

代码:

java 复制代码
import java.util.Arrays;
import java.util.LinkedList;

public class 根据身高重建队列 {
    public static void main(String[] args) {

    }


    public static int[][] reconstructQueue(int[][] people) {
       // 身高从大到小排(身高相同k小的站前面)
        Arrays.sort(people, (a, b) -> {
            if (a[0] == b[0]) return a[1] - b[1];   // a - b 是升序排列,故在a[0] == b[0]的狀況下,會根據k值升序排列
            return b[0] - a[0];   //b - a 是降序排列,在a[0] != b[0],的狀況會根據h值降序排列
        });
        LinkedList<int[]> que = new LinkedList<>();

        for (int[] p : people) {
            que.add(p[1],p);   //Linkedlist.add(index, value),會將value插入到指定index裡。
        }

        return que.toArray(new int[people.length][]);
    }
}
相关推荐
c238561 小时前
把 C++ 内存分配拆透:new 与 malloc 的三层血缘
开发语言·c++·算法
aaaameliaaa2 小时前
指针之总结
c语言·笔记·算法
宵时待雨2 小时前
优选算法专题9:哈希表
数据结构·算法·散列表
txzrxz2 小时前
最短路问题——Dijkstra 算法
数据结构·c++·算法·最短路·优先队列
gwf2162 小时前
磨损均衡算法(Wear Leveling)——SSD如何让每块闪存“公平退休“?
运维·数据库·人工智能·python·嵌入式硬件·算法·智能硬件
noipp3 小时前
推荐题目:洛谷 P6231 [JSOI2013] 公交系统
c语言·数据结构·c++·算法·游戏·洛谷·luogu
c238563 小时前
C/C++每日一练6
c语言·c++·算法
AI小码3 小时前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程
KaMeidebaby5 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
alphaTao5 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode