力扣55. 跳跃游戏(动态规划)

Problem: 55. 跳跃游戏

文章目录

题目描述

思路

我们将问题稍做转换每次求取当前位置可以走到的最远位置 ,在此基础上我们将最终判断是否能走出整个nums;同时我们要判断中途会不会遇到某个位置是0使得不能继续走下去

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为数组nums的大小

空间复杂度:

O ( 1 ) O(1) O(1);

Code

cpp 复制代码
class Solution {
public:
    /**
     * Dynamic programming
     * 
     * @param nums Given array
     * @return bool
     */
    bool canJump(vector<int>& nums) {
        int n = nums.size();
        int farthest = 0;
        for (int i = 0; i < n - 1; ++i) {
            farthest = max(farthest, i + nums[i]);
            //Meet to 0
            if (farthest <= i) {
                return false;
            }
        }
        return farthest >= n - 1;
    }
};
相关推荐
Nil2081 小时前
leetcode 416分割等和子集
算法·leetcode·职场和发展
Martina_03212 小时前
AI生成3D模型导入后只剩一个大网格?用6步完成微缩场景拆分与优化
人工智能·游戏·数学建模·3d·ai·自然语言处理·aigc
y1su3 小时前
Leetcode 二分模板
java·数据结构·算法·leetcode·排序算法
Pniubi4 小时前
力扣55跳跃游戏(贪心)
算法·leetcode·游戏
棉猴5 小时前
玩游戏学Python4-字符串
python·游戏·字符串·pygame·玩游戏
释厄6235 小时前
智能体释放度子集律——文本 ⊂ 音频 ⊂ 视频⊂ 游戏
开发语言·游戏·microsoft·音视频
CoderYanger6 小时前
A.每日一题:856. 括号的分数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
All for pursuit.7 小时前
【动态规划-8】152.乘积最大子数组
数据结构·c++·算法·leetcode·动态规划
Nil2088 小时前
leetcode 152乘积最大子数组
leetcode
夏幻灵19 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表