算法奇妙屋(四十一)-贪心算法学习之路 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());
    }
}
相关推荐
arvin_xiaoting5 小时前
OpenClaw学习总结_III_自动化系统_3:CronJobs详解
数据库·学习·自动化
_日拱一卒5 小时前
LeetCode:合并区间
算法·leetcode·职场和发展
汀、人工智能5 小时前
[特殊字符] 第3课:最长连续序列
数据结构·算法·数据库架构·图论·bfs·最长连续序列
Kethy__5 小时前
计算机中级-数据库系统工程师-数据结构-图
数据结构·算法·软考··数据库系统工程师·计算机中级
亿秒签到6 小时前
L2-007 家庭房产
数据结构·c++·算法
arvin_xiaoting6 小时前
OpenClaw学习总结_III_自动化系统_2:Webhooks详解
运维·学习·自动化
paeamecium6 小时前
【PAT甲级真题】- Longest Symmetric String (25)
数据结构·c++·算法·pat考试
不早睡不改名@6 小时前
Netty源码分析---Reactor线程模型深度解析(一)
java·笔记·学习·netty
祁白_7 小时前
Bugku:备份是一个好习惯
笔记·学习·web安全·ctf