LeetCode算法练习top100:(10)贪心算法

java 复制代码
package top100.贪心算法;

import java.util.ArrayList;
import java.util.List;

public class TOP {
    //121. 买卖股票的最佳时机
    public int maxProfit(int[] prices) {
        int res = 0, min = prices[0];
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < min) {
                min = prices[i];
            }
            res = Math.max(res, prices[i] - min);
        }
        return res;
    }

    //55. 跳跃游戏
    public boolean canJump(int[] nums) {
        if (nums == null || nums.length == 0) {
            return true;
        }
        int max = 0; //每个位置可达的最大索引
        for (int i = 0; i < nums.length ; i++) {
            if (max >= i) { //保证i可达
                max = Math.max(max, nums[i] + i);
            }
        }
        return max >= nums.length - 1; //最大索引超出最后一个字符索引,则可达
    }

    //45. 跳跃游戏 II
    //贪心算法:让每次跳到最远,则跳跃次数可以最小
    //计算当前位置可以跳跃的范围,从范围里面选取一个可以跳跃的最远位置作为下一个位置
    public int jump(int[] nums) {
        int res = 0;
        int n = nums.length;
        for (int i = 0; i < n - 1;) {
            //每次跳跃选取下个阶段可以跳跃的最远值
            int maxIndex = nums[i] + i; //下个阶段可以跳跃的最大范围
            //直接跳到最后了
            if (maxIndex >= n - 1) {
                res++;
                break;
            }
            //选择下个跳跃范围内可以跳跃到的最远值
            int max = nums[i + 1] + i + 1;//下个范围跳的最远距离
            int index = i + 1; //最大数对应的位置
            for (int j = i + 2; j <= maxIndex; j++) {
                if (nums[j] + j >= max) { //如果相等,取后面一个
                    max = nums[j] + j;
                    index = j; //每次在上次能跳到的范围(end)内选择一个能跳的最远的位置(也就是能跳到max_far位置的点)作为下次的起跳点 !
                }
            }
            i = index; //跳跃到最大位置上
            res++;
        }
        return res;
    }

    //763. 划分字母区间
    //贪心算法:先根据当前字符,寻找其最大索引,在这个字串中,寻找每一个字符的最大索引,截取为一个字串
    public List<Integer> partitionLabels(String s) {
        List<Integer> res = new ArrayList<>();
        //用map记录每个字符的最大索引
        int[] map = new int[26];
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            map[chars[i] - 'a'] = i;
        }
        for (int i = 0; i < chars.length;) {
            //当前字母的最大索引
            int maxIndex = map[chars[i] - 'a'];
            //更新字串中的maxIndex
            for (int j = i + 1; j < maxIndex; j++) {
                int curMax = map[chars[j] - 'a'];
                maxIndex = Math.max(maxIndex, curMax);
            }
            //截取为字串
            res.add(maxIndex - i + 1);
            i = maxIndex + 1; //更新索引
        }
        return res;
    }
}
相关推荐
和裕43 分钟前
蜂窝板 vs 七层瓦楞重型纸箱:大件工业设备运输性能与成本全对比
大数据·运维·网络·人工智能·算法
衡石科技1 小时前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析
人工智能·科技·学习·算法·企业级bi
Msshu1232 小时前
2~5串锂电快充优选|XSP36 Type‑C升压/升降压快充管理芯片
数据结构·随机森林·贪心算法·排序算法·线性回归·启发式算法
shirsl2 小时前
算法 Day 2 滑动窗口 + 栈 / 单调栈
开发语言·python·算法
土司大王3 小时前
LeetCode hot100 回溯专题总结:Java 通用模板、决策树模型
java·算法·leetcode·决策树
2601_962218613 小时前
万象生鲜系统订单全生命周期状态同步技术实现业务可视
大数据·数据库·人工智能·python·算法
那年窗外下的雪.4 小时前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计
服务器·网络·学习·算法·哈希算法
wzdark5 小时前
基于启发式搜索的最优路径规划算法研究4
算法
l1t5 小时前
用superpi、tinypi、tpi等工具计算圆周率的比较
c语言·算法
a187927218315 小时前
【算法】链表(二):链表上的双指针——变速、异链与定距,和一份路程账本
数据结构·算法·leetcode·链表·go·指针·环形链表