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

比较好的外贸网站广州建设厅官方网站

比较好的外贸网站,广州建设厅官方网站,wordpress网站搜不到,盘锦如何做百度的网站既股票买卖系列之后的第二组贪心算法题目:跳跃游戏系列。这一篇介绍的两个问题,其输入均为一个数组,每个元素表示在该位置可以跳跃的最大长度。 55. Jump Game You are given an integer array nums. You are initially positioned at the …

既股票买卖系列之后的第二组贪心算法题目:跳跃游戏系列。这一篇介绍的两个问题,其输入均为一个数组,每个元素表示在该位置可以跳跃的最大长度。

55. Jump Game

You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

问题描述

给定一个非负整数数组,每个元素表示在该位置可以跳跃的最大长度。判断是否能够到达最后一个下标。

贪心策略

  • 维护一个最远可到达的位置 max_reach
  • 遍历数组,更新 max_reach
  • 如果 max_reach 超过最后一个下标,则返回 True

解题思路

这是一个典型的贪心算法问题。我们可以通过维护一个变量 max_reach 来记录当前能够到达的最远位置。遍历数组时,更新 max_reach,并检查是否能够到达或超过最后一个下标。

步骤

  • 初始化 max_reach = 0,表示当前能够到达的最远位置。
  • 遍历数组 nums
    • 如果当前位置 i 超过了 max_reach,说明无法到达当前位置,返回 false
    • 更新 max_reachmax(max_reach, i + nums[i])
    • 如果 max_reach 已经大于或等于最后一个下标,返回 true
  • 遍历结束后,如果 max_reach 大于或等于最后一个下标,返回 true,否则返回 false
bool canJump(vector<int>& nums) {int max_reach = 0;for (int i = 0; i < nums.size(); i++) {if (max_reach < i) {return false;}max_reach = max(max_reach, i + nums[i]);if (max_reach >= nums.size() - 1) {return true;}}if (max_reach >= nums.size() - 1) {return true;}else {return false;}
}

复杂度分析

  • 时间复杂度:只需要遍历数组一次,时间复杂度为 O ( n ) O(n) O(n),其中 n n n 是数组的长度。
  • 空间复杂度:只使用了常数级别的额外空间,空间复杂度为 O ( 1 ) O(1) O(1)

45. Jump Game II

You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

  • 0 <= j <= nums[i] and
  • i + j < n

Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

解题思路

这是一个典型的贪心算法问题。我们需要找到到达最后一个下标的最小跳跃次数。可以通过维护两个变量来解决:

  • 当前跳跃范围:[start, end],表示当前跳跃可以到达的范围。
  • 最远可到达位置:max_reach,表示在当前跳跃范围内,能够到达的最远位置。

步骤

  • 初始化:
    • jumps = 0:记录跳跃次数。
    • end = 0:当前跳跃范围的结束位置。
    • max_reach = 0:当前跳跃范围内能够到达的最远位置。
  • 遍历数组 nums
    • 更新 max_reachmax(max_reach, i + nums[i])
    • 如果当前位置 i 到达了当前跳跃范围的结束位置 end
      • 增加跳跃次数 jumps++
      • 更新 endmax_reach,表示进入下一个跳跃范围。
  • 返回 jumps
int jump(vector<int>& nums) {int jumps = 0;      // 记录跳跃次数int end = 0;        // 当前跳跃范围的结束位置int max_reach = 0;  // 当前跳跃范围内能够到达的最远位置for (int i = 0; i < nums.size() - 1; i++) {max_reach = max(max_reach, i + nums[i]);// 如果当前位置到达了当前跳跃范围的结束位置if (i == end) {jumps++;       // 增加跳跃次数end = max_reach; // 更新跳跃范围}}return jumps;
}

复杂度分析

  • 时间复杂度:只需要遍历数组一次,时间复杂度为 O ( n ) O(n) O(n),其中 n n n 是数组的长度。
  • 空间复杂度:只使用了常数级别的额外空间,空间复杂度为 O ( 1 ) O(1) O(1)
http://www.yayakq.cn/news/539699/

相关文章:

  • 在自己网站做支付可以吗济南媒体邀约
  • 珠海网页模板建站两学一做纪实评价系统登陆网站
  • 免费建站网站一级大录像不卡在线看网页上海网站建设公司电话
  • 设计网站大全软件网络服务器配置与管理考试题
  • 怎样做学校网站wordpress中文表单生成
  • 建电影网站赚钱挣钱吗缔烨建设公司网站
  • 商城网站建设推荐微信怎么弄自己的商城
  • 国内好用的五款开源建站系统网站开发与维护课程设计
  • 网站开发人员介绍做导购网站
  • node做网站优势江宁网站建设多少钱
  • 做网站前台内容对应填充网站建设好卖吗
  • 仿同程网 连锁酒店 网站模板想做企业网站
  • 红酒商城网站建设福州网站定制设计
  • 龙华做网站东营定制网站建设服务
  • 网站建设com网站二级建造师最好的网站
  • 网站建设选哪家深圳万齐创享网站建设
  • 乾安网站建设公司网址导航系统
  • 盐城网站开发本地开发app的公司地址
  • 网站管理助手建站wordpress 会员聊天
  • 乡村旅游网站建设的意义男女做羞羞完整版网站
  • 如何制作网站的步骤怎样破解网站后台密码
  • 成都网站快速排名seo承诺排名的公司
  • 东莞学校网站建设在线做c语言题目的网站
  • 怎么制作网站来赚钱网站怎么运营推广
  • 临沂设计公司有哪些利用小说网站做本站优化
  • seo网站优化流程国外看新闻app推荐
  • 摄影网站参考文献上海企业网站建设报价
  • 天津建设网网站打不开国外优秀购物网站
  • 哪里有网站建设工程怎么做网站网站的代理
  • 江苏手机网站建设中国网站访问量排行