力扣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;
    }
};
相关推荐
yuanpan3 小时前
Python Pygame 入门教程:从零学会创建窗口、绘图和游戏交互
python·游戏·pygame
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 141. 环形链表 | C++ 哈希表直觉解法
c++·leetcode·链表
熊猫钓鱼>_>5 小时前
AR游戏的“轻”与“深”:当智能体接管眼镜,游戏逻辑正在发生什么变化?
人工智能·游戏·ai·ar·vr·game·智能体
张老师带你学6 小时前
Unity 食物 农产品相关
科技·游戏·unity·游戏引擎·模型
谭欣辰7 小时前
详细讲解 C++ 状压 DP
开发语言·c++·动态规划
北顾笙9808 小时前
day35-数据结构力扣
数据结构·算法·leetcode
ulias2129 小时前
leetcode热题 - 4
算法·leetcode·职场和发展
圣保罗的大教堂9 小时前
leetcode 1559. 二维网格图中探测环 中等
leetcode
_日拱一卒10 小时前
LeetCode:148排序链表
算法·leetcode·链表
生信研究猿10 小时前
leetcode 78.子集
算法·leetcode·深度优先