力扣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;
    }
};
相关推荐
●VON6 小时前
谁小时候不想要一个游戏修改器?我把电脑 Skill 做成了 Android 小助手
android·游戏·电脑
刃神太酷啦6 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
wenyq711 小时前
LeetCode 2904. Shortest and Lexicographically Smallest Beautiful String
算法·leetcode
本原财经13 小时前
卖游戏、筹H股,昆仑万维临IPO磨AI含金量
人工智能·游戏
_Narcissus_13 小时前
分治&递归
数据结构·c++·笔记·算法·leetcode·递归·分治
青 春 记 忆16 小时前
LeetCode 283. 移动零|Python 解法详解
python·算法·leetcode
FairGuard手游加固17 小时前
iOS 游戏加固技术解析:代码混淆、资源加密、内存保护与重签名检测
安全·游戏·macos·unity·ios·objective-c
evans在进步20 小时前
LeetCode 189 轮转数组:三次反转为什么能实现右旋?
数据结构·算法·leetcode