力扣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;
    }
};
相关推荐
阿里嘎多哈基米29 分钟前
速通Hot100-Day09——二叉树
算法·leetcode·二叉树·hot100
Frostnova丶32 分钟前
LeetCode 48 & 1886.矩阵旋转与判断
算法·leetcode·矩阵
多打代码32 分钟前
2026.3.22 回文子串
算法·leetcode·职场和发展
im_AMBER1 小时前
Leetcode 144 位1的个数 | 只出现一次的数字
学习·算法·leetcode
小刘不想改BUG1 小时前
LeetCode 138.随机链表的复制 Java
java·leetcode·链表·hash table
参.商.2 小时前
【Day43】49. 字母异位词分组
leetcode·golang
白宇横流学长2 小时前
基于UE引擎的格斗类游戏《SE2》的开发与实现
游戏
风酥糖2 小时前
Godot游戏练习01-第16节-游戏中的状态机
算法·游戏·godot
参.商.2 小时前
【Day45】647. 回文子串 5. 最长回文子串
leetcode·golang
Trouvaille ~2 小时前
【优选算法篇】哈希表——空间换时间的极致艺术
c++·算法·leetcode·青少年编程·蓝桥杯·哈希算法·散列表