力扣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;
    }
};
相关推荐
yi01125 分钟前
DAY17: LeetCode 139|单词拆分:从“把单词删掉”到用 DP 记录合法切口
人工智能·笔记·python·算法·leetcode·动态规划
Navigator_Z3 小时前
LeetCode //C - 1269. Number of Ways to Stay in the Same Place After Some Steps
c语言·算法·leetcode
垆边人似月.4 小时前
华为算法题:最长连续递增子序列的变
数据结构·算法·leetcode
longlongzihan4 小时前
LeetCode 56合并区间:排序与贪心
c++·算法·leetcode
kukubuzai5 小时前
二分查找系列一
数据结构·算法·leetcode
老歌老听老掉牙5 小时前
动态规划:从递归到最优决策的算法艺术
算法·动态规划
郝学胜-神的一滴6 小时前
游戏引擎原理与实践 02:揭开3A游戏背后的技术面纱
开发语言·计算机网络·程序人生·游戏·游戏引擎
全速向光15 小时前
盖世游戏PC模拟器:手机也能畅玩电脑平台游戏
游戏·智能手机
Minecraft红客16 小时前
以撒的结合
python·游戏·电脑·娱乐