当前位置: 首页 > news >正文

45. Jump Game II

目录

题目描述

贪心


题目描述

45. Jump Game II

贪心

正向查找可到达的最大位置

时间复杂度O(n)

class Solution {
public:int jump(vector<int>& nums) {int n = nums.size();if(n == 1)return 0;int cur_cover = 0;int cover = 0;int res = 0;for(int i = 0;i <= cover;i++){if(nums[i]+i > cover){cover = nums[i]+i;}if(i == cur_cover){res++;cur_cover = cover;if(cur_cover >= n-1)break;}}return res;}
};

反向查找出发位置

时间复杂度O(n^2)

class Solution {
public:int jump(vector<int>& nums) {int res = 0;int n = nums.size();int destination = n-1;while(destination > 0){for(int i = 0; i<=destination;i++){if(i+nums[i] >= destination){destination = i;res++;break;}}}return res;}
};
http://www.xdnf.cn/news/985375.html

相关文章:

  • OPenCV CUDA模块图形变换----构建透视变换映射表函数buildWarpPerspectiveMaps()
  • AUTOSAR实战教程--DoIP_03_ 代码分析与调用栈观测(ISOLAR版)
  • 详解多模态
  • 项目制造型企业如何实现高效管理?从金属工具制造说起
  • 【LeetCode 热题 100】只出现一次的数字 / 多数元素 / 颜色分类 / 寻找重复数
  • 建造者模式(Builder Pattern)
  • 设计模式-组合模式
  • Ubuntu20.04更新Cmake版本
  • 找到字符串中所有字母异位词
  • 使用 PyTorch 和 TensorBoard 实时可视化模型训练
  • SpringBoot学习day1-SpringBoot的简介与搭建
  • Phthon3 学习记录-0611
  • Windows 删除文件出现错误代码0x80070570:文件或目录损坏且无法读取
  • 第五章网络管理
  • vibe coding 2025工具全景图
  • 构建高效开发节奏:我的IDEA休息提醒插件实践
  • fastadmin自动保存格式datetime
  • JavaEE-SpringBoot
  • 基于SpringBoot实现的课程答疑系统设计与实现【源码+文档】
  • 【MySQL数据库 | 第四篇】 数据类型+DDL表操作1
  • 从零开始了解数据采集技术篇(2)——如何提高数据采集的精度与速度
  • ALIGN_COMMA_ENABLE参数
  • 贪心选择 (Greedy Choice)
  • 大语言模型智能体开发的技术框架与应用前景
  • 日期的数据格式转换
  • 红队手法:从web漏洞到ssh横向移动 实战方案
  • vue3笔记(1)自用
  • 选择、填空、判断
  • 深入理解Python协程:async def、async for、await、yield详解
  • 学习日记-day27-6.11