力扣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;
    }
};
相关推荐
xinhuanjieyi13 小时前
html修复游戏种太阳错误
前端·游戏·html
xhtdj14 小时前
智源大会圆桌大模型没有终局具身智能可能是中国的 AlphaGo 时刻
人工智能·clickhouse·安全·动态规划
魔士于安15 小时前
unity 音乐会场景 unity2022
游戏·unity·游戏引擎·贴图·模型
人道领域18 小时前
【LeetCode刷题日记】47.全排列Ⅱ
java·开发语言·算法·leetcode
Navigator_Z18 小时前
LeetCode //C - 1095. Find in Mountain Array
c语言·算法·leetcode
码来的小朋友18 小时前
[python] 我开发了一个有20个关卡随机地图的迷宫游戏
python·游戏·pygame
不羁的木木18 小时前
《HarmonyOS 6.1 新能力实战之智感握姿》第四篇:进阶应用——横屏游戏手柄模式
游戏·华为·harmonyos
Swift社区19 小时前
鸿蒙游戏Runtime解析:Store如何驱动整个游戏世界?
游戏·华为·harmonyos
2301_7644413319 小时前
番茄钟+AI:高效专注的秘密武器
人工智能·算法·数学建模·动态规划·交互
jushi899920 小时前
修复电脑常见运行库问题 DirectX 组件状态、运行库、DLL 游戏常见运行库 DirectX 修复工具增强版
游戏·电脑