力扣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;
    }
};
相关推荐
Dream it possible!14 分钟前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试
菜鸟233号3 小时前
力扣377 组合总和 Ⅳ java实现
java·数据结构·算法·leetcode
老鼠只爱大米3 小时前
LeetCode算法题详解 189:轮转数组
leetcode·轮转数组·数组旋转·环状替换法·算法面试题
qq_546937273 小时前
Windows11 26H1 游戏版!
游戏
jinmo_C++4 小时前
Leetcode_59. 螺旋矩阵 II
算法·leetcode·矩阵
夏鹏今天学习了吗4 小时前
【LeetCode热题100(81/100)】零钱兑换
算法·leetcode·职场和发展
2501_948122635 小时前
React Native for OpenHarmony 实战:Steam 资讯 App 服务条款实现
javascript·react native·react.js·游戏·ecmascript·harmonyos
踩坑记录5 小时前
leetcode hot100 238.除了自身以外数组的乘积 medium
leetcode
海天一色y6 小时前
python---力扣数学部分
算法·leetcode·职场和发展
踩坑记录6 小时前
leetcode hot100 56.合并区间 medium
leetcode