力扣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;
    }
};
相关推荐
Pniubi6 分钟前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hkNaruto3 小时前
【AI】基于大模型的AI游戏地图生成、控制方案:AI Native Game Harness 迁移操作手册
前端·人工智能·游戏
揽星河..8 小时前
【C语言】猜数字游戏
c语言·算法·游戏
释厄6239 小时前
AI 军备竞赛悖论——为 AI 卖游戏但 AI 需要游戏母场景
人工智能·游戏·kotlin
旖旎夜光11 小时前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊11 小时前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt11 小时前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊11 小时前
LeetCode1341~1360题目解析
java·leetcode
海绵宝宝转agent12 小时前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
Pniubi13 小时前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展