力扣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;
    }
};
相关推荐
mmmmath_38 小时前
LeetCode.199.二叉树的右视图
数据结构·leetcode
Nil20813 小时前
leetcode 198打家劫舍
算法·leetcode·职场和发展
mmmmath_313 小时前
LeetCode.429.N叉树的层序遍历
数据结构·leetcode
xcLeigh17 小时前
Unity基础:Instantiate实例化——在运行时动态创建游戏物体
游戏·unity·游戏引擎·教程·instantiate
不会就选b18 小时前
算法日常・每日刷题--<动态规划>22
算法·动态规划
棉猴19 小时前
玩游戏学Python2-IDLE与pgzero入门
开发语言·python·游戏·玩游戏·pgzero·pgzrun
INGNIGHT19 小时前
593 · 石头游戏 II (区间dp石子合并 翻倍循环问题)
leetcode
河南花仙子科技19 小时前
加油站小游戏营销拓客新方案
游戏·拓客
aqiu11111119 小时前
【C++算法打怪专栏】LeetCode 402. 移掉 K 位数字(单调栈 + 贪心)
c++·算法·leetcode
第六种自由20 小时前
[路飞]_Leetcode 22. 括号生成
javascript·算法·leetcode