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;
    }
}
相关推荐
罗西的思考6 分钟前
【Agentic RL / 强化学习 / OPD】OpenClaw-RL 源码阅读笔记 --- (4)--- 系统架构
人工智能·算法·机器学习
QiLinkOS7 分钟前
从技术到资产的跃迁:企业专利布局的深层逻辑
c语言·数据结构·c++·单片机·嵌入式硬件·算法·开源
aini_lovee15 分钟前
FMCW雷达测速测距系统(锯齿波 + CFAR检测)
算法
qq_2975746718 分钟前
设计模式系列文章(基础篇第 11 篇):模板方法模式——定义算法骨架,实现代码复用与流程统一
算法·设计模式·模板方法模式
lqqjuly25 分钟前
知识蒸馏:理论、算法与可运行实现
人工智能·深度学习·算法
水上冰石34 分钟前
comfui的sd1.5模型,有多少采样算法,详解每一个采样算法
人工智能·算法
黎阳之光1 小时前
视频孪生+空天地水工融合,黎阳之光构建智慧水利监测新范式
大数据·人工智能·物联网·算法·安全
cheems95271 小时前
[算法手记] 贪心 爬楼梯问题
算法·贪心算法
KaMeidebaby1 小时前
卡梅德生物技术快报|酵母双杂交 cDNA 文库构建与蛋白互作筛选流程
服务器·前端·数据库·人工智能·算法
圣保罗的大教堂1 小时前
leetcode 3300. 替换为数位和以后的最小元素 简单
leetcode