力扣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;
    }
};
相关推荐
小此方13 小时前
[算法实战论:动态规划]回文串问题的dp解法,回文串问题的母题,以及以此为基础的派生题
算法·动态规划
java_logo13 小时前
Docker 部署 EmulatorJS:轻松搭建浏览器复古游戏平台
游戏·docker·容器·复古游戏·轩辕镜像·emulatorjs·红白机
Pniubi14 小时前
力扣560.和为k的子数组
算法·leetcode·职场和发展
Pniubi15 小时前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
文丑颜不良啊20 小时前
LeetCode2081~2100题目解析
java·leetcode
-梅21 小时前
算法(1) 动态规划
算法·动态规划
文丑颜不良啊21 小时前
LeetCode2061~2080题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode1941~1960题目解析
java·leetcode
UIU1142 天前
P1481 魔族密码
c++·算法·动态规划
文丑颜不良啊2 天前
LeetCode1961~1980题目解析
java·leetcode