力扣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;
    }
};
相关推荐
伊玛目的门徒2 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
言乐65 小时前
Python实现可运行解密游戏游戏框架
python·游戏·小程序·游戏程序·关卡设计
旖-旎5 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
怪兽学LLM8 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展
user-猴子9 小时前
不会写代码,做小游戏教程
人工智能·游戏
退休倒计时9 小时前
【每日一题】LeetCode 39. 组合总和 TypeScript
算法·leetcode·typescript
Scabbards_10 小时前
面试Leetcode - Array
leetcode·面试·职场和发展
河南花仙子科技11 小时前
花仙子科技|游戏出海发行与本地化运营避雷指南
科技·游戏
兰令水11 小时前
hot100【acm版】【2026.7.13打卡-java版本】
java·开发语言·数据结构·算法·leetcode·面试
Tisfy12 小时前
LeetCode 1291.顺次数:打表/枚举
算法·leetcode·题解·枚举·遍历