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;
    }
}
相关推荐
冻柠檬飞冰走茶12 分钟前
PTA基础编程题目集 7-20 打印九九口诀表(C语言实现)
c语言·开发语言·数据结构·算法
鱼子星_13 分钟前
《算 · 法》题目解析篇(1):最近公共祖先问题,线段树,最长回文子序列
c++·算法·动态规划·递归
雾喔15 分钟前
算法练习5
算法·代理模式
问商十三载29 分钟前
RAG 检索召回越多越好?2026 信噪比模型深度解析
人工智能·算法·机器学习
额恩6630 分钟前
ResearchPilot 第三阶段总结报告
python·算法
Hi李耶42 分钟前
【LeetCode】9-回文数
算法·leetcode·职场和发展
红叶舞43 分钟前
基于线段树的数据结构
数据结构·python·算法
断点之下2 小时前
从“理扑克牌”到“分组优化”——直接插入排序与希尔排序详解
数据结构·算法·排序算法
起个破名想半天了2 小时前
算法与数据结构之DFS深度优先遍历
数据结构·算法·深度优先
李伟_Li慢慢2 小时前
凸包算法
人工智能·算法