每日一题-贪心算法

122. 买卖股票的最佳时机 II - 力扣(LeetCode)

55. 跳跃游戏 - 力扣(LeetCode)

这个题目一开始肯定是会懵,就比如说一开始先跳几步,之后再怎么跳,其实我们就可以用最大范围来算就行了,用局部最优推出全局最优

复制代码
java 复制代码
class Solution {
    public boolean canJump(int[] nums) {
        if(nums.length == 1){
            return true;
        }
        int cun = 0;
        for(int i = 0;i <= cun;i++){
            cun = Math.max(cun,i+nums[i]);
            if(cun>=nums.length-1){
                return true;
            }
        }
        return false;
    }
}

45. 跳跃游戏 II - 力扣(LeetCode)

这道题目比上一道难不少,主要就是你需要计算那个最少跳跃数,

java 复制代码
class Solution {
    public int jump(int[] nums) {
        int result = 0;
        // 当前覆盖的最远距离下标
        int end = 0;
        // 下一步覆盖的最远距离下标
        int temp = 0;
        for (int i = 0; i <= end && end < nums.length - 1; ++i) {
            temp = Math.max(temp, i + nums[i]);
            // 可达位置的改变次数就是跳跃次数
            if (i == end) {
                end = temp;
                result++;
            }
        }
        return result;
    }
}
相关推荐
信奥卷王5 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵5 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG7 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
码完就睡7 小时前
数据结构——红黑树
数据结构
码爸7 小时前
数据结构详细介绍
数据结构
Navigator_Z7 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛8 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记8 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序8 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm