力扣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;
    }
};
相关推荐
humors22111 小时前
支付宝游戏频道《灵画师》体验
游戏·介绍·体验·攻略·灵画师·试玩
玖玥拾13 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
2501_9423895513 小时前
Epoch AI旗下FrontierMath的负责人Elliot Glazer
数据结构·决策树·动态规划·散列表
HMS Core14 小时前
基于人体骨骼点识别与跟踪,实现低时延体感游戏
游戏·华为·harmonyos
Hi李耶14 小时前
【LeetCode】541.反转字符串 II
算法·leetcode·职场和发展
白白白小纯19 小时前
每日算法day3—回文链表,链表分割
c语言·数据结构·算法·leetcode
zander25820 小时前
35. 搜索插入位置:从边界语义理解二分查找
数据结构·算法·leetcode
yyds_yyd_1008621 小时前
3731. 找出缺失的元素(2026.08.04)
c++·leetcode
lueluelue471 天前
LeetCode:链表
算法·leetcode·链表
橘子汽水1681 天前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表