力扣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;
    }
};
相关推荐
Hello_Pyhx19 小时前
Pavise v3.3.0.8 配置指南:从游戏识别到会话恢复
游戏
希望我万事顺遂20 小时前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode
是main不是漫21 小时前
【双指针|LeetCode1089 复写零】巧用逆向思路,破解原地覆盖陷阱
数据结构·算法·leetcode
Pniubi2 天前
力扣76.最小覆盖子串
算法·leetcode·职场和发展
文丑颜不良啊2 天前
LeetCode2541~2560题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2481~2500题目解析
java·leetcode
johnsong3 天前
Claude Fable 5.2 登顶:65 分、单发做游戏,和一份 $26.54 一单的账单
人工智能·游戏
文丑颜不良啊3 天前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode2521~2540题目解析
java·leetcode