力扣45. 跳跃游戏 II

Problem: 45. 跳跃游戏 II

文章目录

题目描述

思路

Problem: 55.跳跃游戏
该题在上述的基础上,我们每次先求取当前可跳区间内的最远距离farthest;每当走到当前的区间胃部时(end == i):跳跃步数加一(jumps++),同时将下一次的可跳的最远区间更新(end = farthest;)

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为数组nums的大小

空间复杂度:

O ( 1 ) O(1) O(1)

Code

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        int n = nums.size();
        int end = 0;
        int farthest = 0;
        int jumps = 0;
        for (int i = 0; i < n - 1; ++i) {
            farthest = max(farthest, i + nums[i]);
            if (end == i) {
                jumps++;
                end = farthest;
            }
        }
        return jumps;
    }
};
相关推荐
猿人谷6 小时前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
jump_jump6 小时前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
复杂网络7 小时前
Stable Diffusion 视觉大模型微调技术深度调研
算法
复杂网络7 小时前
基于 Stable Diffusion 架构的视觉大模型代表性工作与原理深度解析
算法
MrZhao4007 小时前
Agent Loop 如何用 Hook 扩展:权限、日志与工具拦截
算法
MrZhao4007 小时前
Agent 为什么需要 Skills:别把所有知识都塞进 system prompt
算法
XIAOHEZIcode1 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
JieE2122 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
Aloys_Code2 天前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴
金銀銅鐵2 天前
用 Python 实现 Take-Away 游戏
python·游戏