力扣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;
    }
};
相关推荐
-森屿安年-11 小时前
63. 不同路径 II
c++·算法·动态规划
两水先木示11 小时前
【Unity3D】小游戏启动优化、发热优化、蒙皮网格优化
游戏
想吃火锅100511 小时前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展
资源分享助手12 小时前
杀戮尖塔2下载、Slay the Spire 2中文版、卡牌肉鸽游戏、杀戮尖塔2联机、杀戮尖塔2攻略
游戏
凌波粒14 小时前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
退休倒计时14 小时前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript
小欣加油15 小时前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒15 小时前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
凌波粒16 小时前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode
吃着火锅x唱着歌16 小时前
LeetCode 2530.执行K次操作后的最大分数
数据结构·算法·leetcode