力扣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;
    }
};
相关推荐
Zootopia6262 小时前
多架 eVTOL集群排班调度方案思考
人工智能·算法·数学建模·matlab·动态规划·无人机·evtol
0+1114 小时前
算法 --模拟
c++·算法·leetcode
随意起个昵称5 小时前
【单调队列优化DP】U301134 绿色通道
算法·动态规划
郝学胜-神的一滴5 小时前
AI 编程智能体 03:拆解当下主流智能体能力
开发语言·c++·人工智能·python·程序人生·游戏
Nil2085 小时前
leetcode 739每日温度
算法·leetcode·职场和发展
xqqxqxxq6 小时前
LeetCode Hot100 矩阵专题题解笔记
笔记·leetcode·矩阵
All for pursuit.7 小时前
【贪心-4】581.最短无序连续子数组
数据结构·c++·算法·leetcode
mmmmath_38 小时前
LeetCode.020.有效的括号
数据结构·算法·leetcode
All for pursuit.9 小时前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode
longlongzihan9 小时前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口