算法奇妙屋(四十一)-贪心算法学习之路 8

文章目录

一. 力扣 134. 加油站

1. 题目解析

这里要求从i出发, 最后还要能回到i, 形成回路

2. 算法原理

这道题先写出暴力(枚举)解法, 然后在其基础上进行优化, 即可将时间复杂度降为O(N)

3. 代码

暴力解法(超时)

java 复制代码
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            diff[i] = gas[i] - cost[i];
        }
        for (int i = 0; i < n; i++) {
            int profit = 0;
            int j = 0;
            for (; j <= n; j++) {
                profit += diff[(i + j) % n];
                if (profit < 0) {
                    break;
                }
            }
            if (profit >= 0) {
                return i;
            }
        }
        return -1;
    }
}

贪心优化

java 复制代码
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int[] diff = new int[n];
        for (int i = 0; i < n; i++) {
            diff[i] = gas[i] - cost[i];
        }
        for (int i = 0; i < n; i++) {
            int profit = 0;
            int j = 0;
            for (; j <= n; j++) {
                profit += diff[(i + j) % n];
                if (profit < 0) {
                    break;
                }
            }
            if (profit >= 0) {
                return i;
            }
            i = i + j; // 优化
        }
        return -1;
    }
}

二. 力扣 738. 单调递增的数字

1. 题目解析

这道题的暴力解法很好想出来, 从当前数开始逐次递减, 直到每个位置都呈现递增即可

2. 算法原理

暴力解法是将数字变为字符串, 通过比较每个位置的字符即可, 这里图解只画出贪心解法

3. 代码

暴力解法代码(超时)

java 复制代码
class Solution {
    public int monotoneIncreasingDigits(int n) {
        while (n > 9) {
            StringBuilder s = new StringBuilder();
            s.append(n);
            int i = 1;
            for (; i < s.length(); i++) {
                if (s.charAt(i) < s.charAt(i - 1)) break;
            }
            if (i == s.length()) {
                return n;
            }
            n--;
        }
        return n;
    }
}

贪心解法代码

java 复制代码
class Solution {
    public int monotoneIncreasingDigits(int n) {
        StringBuilder s = new StringBuilder();
        s.append(n);
        int len = s.length();
        int i = 0;
        // 找最右面呈递减趋势的位置
        for (; i < len - 1; i++) {
            if (s.charAt(i) > s.charAt(i + 1)) break;
        }
        if (i == len - 1) {
            return n;
        }
        // 找最左边相同的数字位置
        char ch = s.charAt(i);
        int j = i;
        for (; j > 0; j--) {
            if (s.charAt(j - 1) != ch) break;
        }
        s.replace(j, j + 1, String.valueOf((char)(ch - 1)));
        // 该位置之后都变为9
        for (int k = j + 1; k < len; k++) {
            s.replace(k, k + 1, String.valueOf(9));
        }
        return Integer.valueOf(s.toString());
    }
}
相关推荐
Dola_Zou4 小时前
工厂智能排产软件算法保护与按产线计费实战
算法·自动化·软件工程·软件加密
传奇开心果编程4 小时前
【xilem0.4基础语法学与练】第13课:Xilem 0.4 最简短代码体现“一切皆设计图“
学习·rust·前端框架
小雪崩5 小时前
嵌入式学习 day45:51单片机基础
学习·51单片机
啦啦啦啦啦zzzz6 小时前
Logger的组装(c++20)
c++·算法·c++20
尾善爱看海7 小时前
前端算法与手写题集
前端·算法
彧azz7 小时前
Java学习语法篇:变量
java·学习
Yanjun2i7 小时前
Agent学习记录五:Pydantic验证
人工智能·python·学习
程序员阿鹏7 小时前
为什么MySQL InnoDB选择B+树?
数据结构·数据库·b树·sql·mysql·算法·缓存
AI 思录8 小时前
Prompt 事故档案(八):日常表达被标为“待校准”,AI 的爹味语法从哪里来
大数据·人工智能·算法·prompt·用户体验·ai合规
月光船幽幽8 小时前
跨范式映射的稳定接口设计
人工智能·python·算法