news 2026/4/2 21:36:59

贪心算法学习C++

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
贪心算法学习C++

【题目描述】

在给定的一个nums数组中,nums[i]表示从当前i位置最多可以向后跳跃nums[i]个位置。问跳跃到最后 数组最后一个元素的最少跳跃次数???

【贪心】

首先,需要注意的是,题目的意思是最远跳跃nums[i]个位置,而不是必须跳跃mun[i]个位置。以一个例子来看:nums=[2,3,1,1,4,2,6,7,1]。nums[0]=2,所以可以从0位置最多向后跳跃2个位置,所以最远可以跳跃到1,也可以选择跳跃到3。


nums=[2,3,1,1,4,2,6,7,1]。还是以这个数组为例。

第一次的起跳位置是数组下标为0的位置。经过一次跳跃之后,可以到达3或者1,此时的位置是第二次起跳位置,也就是说第二次起跳的位置是3或者1。同理,假设3为第二次起跳的位置 ,那么可以到达【1,1,4】,假设1为第二次起跳的位置,可以到达【1】。两者取并集,可以得到从第二次起跳位置起跳,可以跳跃到的区间。后面也是同理,配图:

从上图我们可以发现二次起跳的位置【3,1】和三次起跳的位置【1,1,4】这两个区间存在交集。而题目中求的是最少跳跃次数,所以可以通过二次跳跃可以到达的位置,不会选择跳跃三次。


那么如何判断跳跃到数组的最后一个元素了?对于每一个跳跃区间,我们可以使用两个变量来维护这两个区间的开始位置和结束位置【left,right】,比如 对于二次起跳的位置(上图中蓝色部分的线),对应的区间就是【1,2】。当right超出数组的长度,就表示可以跳跃到数组的结尾位置了。注意:这个【left,right】区间存储的是下标,不是元素。


维护【left,right】区间,从上图可以发现,第三次起跳的【left,right】,其中left可以通过上一次起跳的【left,right】的right+1得来。而right就需要遍历第二次起跳的【left,right】找出最远的距离。

【代码】

只需遍历数组一遍,时间复杂度为O(N)

代码语言:javascript

AI代码解释

class Solution { public: int jump(vector<int>& nums) { int n=nums.size(); int maxpos=0;//存储下一次起跳的最远位置,也就是下一个right int ret=0;//记录最终结果 int left=0,right=0; while(left<=right) { if(maxpos>=n-1)//已经可以跳跃到数组结尾 { return ret; } //遍历当前[left,right],更新下一次的[left,right] for(int i=left;i<=right;i++) { maxpos=max(maxpos,nums[i]+i); } left=right+1; right=maxpos; ret++;//跳跃次数++ } return ret; } };

2,跳跃游戏I

题目连接:55. 跳跃游戏 - 力扣(LeetCode)

本题与上体思路一模一样,只是返回值修改以下即可。

代码语言:javascript

AI代码解释

class Solution { public: bool canJump(vector<int>& nums) { //贪心 int n=nums.size(); int left=0,right=0,ret=0; int maxPos=0;//记录下一个最远跳到的位置 while(left<=right) { if(maxPos>=n-1) return true; //跟新maxPos for(int i=left;i<=right;i++) maxPos=max(maxPos,nums[i]+i); //更新区间 left=right+1; right=maxPos; ret++; } return false; } };

3,加油站

题目连接:134. 加油站 - 力扣(LeetCode)

【题目描述】

题目简单的理解,n个加油站,每个加油站有gas[i]升汽油。刚开始汽车的油箱为空。从第i个加油站行驶到第i+1个加油站,需要消耗cost[i]升汽油。问,是否存在一个加油站,是该汽车可以环绕n个加油占一周,回到起始加油站,返回该加油站的下标。

【暴力枚举算法】

我们可以枚举每个加油站位置,看看是否可以环绕一周,回到起始位置。

从第i个加油站到达第i+1个加油站,需要消耗cost[i]升汽油,而第i个加油站可以获得gas[i]升汽油,所以汽车要想从第i个加油站行驶到第i+1个加油站,必须保证gas[i]>=cos[i]。

我们可以将gas[i]-cos[i]的值保存于一个diff数组中(这个数组不需要定义出来,只是为了好理解),如果从第i个加油站开始走,那么mixup满足gas[i]>=cos[i],也就是diff[i]>=0。


以题目中给的示例为例:

当我们选中一个位置作为起点,从当前位置,再次枚举步数(0~n-),以为可能下标会越界,所以需要在对数组长度取模。

【暴力代码】(会超时)

时间复杂度O(N^2)

代码语言:javascript

AI代码解释

class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int n = gas.size(); //枚举起点 for(int i=0;i<n;i++) { int rest=0;//剩余汽油 for(int step=0;step<n;step++)//枚举步数 { int index=(step+i)%n;//求出走step后的下标 rest+=gas[index]-cost[index]; if(rest<0) break; } if(rest>=0) return i; } return -1; } };

【贪心优化】

代码语言:javascript

AI代码解释

class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int n = gas.size(); //枚举起点 for(int i=0;i<n;i++) { int rest=0;//剩余汽油 int step=0; for(;step<n;step++)//枚举步数 { int index=(step+i)%n;//求出走step后的下标 rest+=gas[index]-cost[index]; if(rest<0) break; } if(rest>=0) return i; i+=step;//循环会完成+1工作 } return -1; } };

www.dongchedi.com/article/7593861948883010110
www.dongchedi.com/article/7593860184296997401
www.dongchedi.com/article/7593857995939824190
www.dongchedi.com/article/7593858058859037208
www.dongchedi.com/article/7593856188576465432
www.dongchedi.com/article/7593856799745524286
www.dongchedi.com/article/7593854395277001241
www.dongchedi.com/article/7593853248357089816
www.dongchedi.com/article/7593849479917847102
www.dongchedi.com/article/7593850791879016985
www.dongchedi.com/article/7593849102899757593
www.dongchedi.com/article/7593846341370102297
www.dongchedi.com/article/7593845182169268761
www.dongchedi.com/article/7593844699736621592
www.dongchedi.com/article/7593846059231855166
www.dongchedi.com/article/7593845002074587673
www.dongchedi.com/article/7593845199110029886
www.dongchedi.com/article/7593844109958234648
www.dongchedi.com/article/7593843358104584766
www.dongchedi.com/article/7593841598811832894
www.dongchedi.com/article/7593843060308967998
www.dongchedi.com/article/7593841492519715353
www.dongchedi.com/article/7593841963301012030
www.dongchedi.com/article/7593838407353729561
www.dongchedi.com/article/7593831928227267096
www.dongchedi.com/article/7593830985880879641
www.dongchedi.com/article/7593832895307973182
www.dongchedi.com/article/7593828800342360601
www.dongchedi.com/article/7593829728961020441
www.dongchedi.com/article/7593829195726258712
www.dongchedi.com/article/7593829391919202841
www.dongchedi.com/article/7593829122569306649
www.dongchedi.com/article/7593828972870271550
www.dongchedi.com/article/7593826313157820953
www.dongchedi.com/article/7593822015514313241
www.dongchedi.com/article/7593824624384180760
www.dongchedi.com/article/7593822602171646488
www.dongchedi.com/article/7593822015513952793
www.dongchedi.com/article/7593822160830218776
www.dongchedi.com/article/7593812639475155518
www.dongchedi.com/article/7593862391214047806
www.dongchedi.com/article/7593856670141284889
www.dongchedi.com/article/7593857060190798398
www.dongchedi.com/article/7593856931858022937
www.dongchedi.com/article/7593854937294275096
www.dongchedi.com/article/7593853687676715582
www.dongchedi.com/article/7593852941996393022
www.dongchedi.com/article/7593851894985081368
www.dongchedi.com/article/7593850569647997502
www.dongchedi.com/article/7593848374730899993
www.dongchedi.com/article/7593846886513738265
www.dongchedi.com/article/7593846450732483097
www.dongchedi.com/article/7593846491957985854
www.dongchedi.com/article/7593843364807148056
www.dongchedi.com/article/7593843093477605913
www.dongchedi.com/article/7593842919485276696
www.dongchedi.com/article/7593843735868637721
www.dongchedi.com/article/7593841312735003161
www.dongchedi.com/article/7593843018189488702
www.dongchedi.com/article/7593840609178370585
www.dongchedi.com/article/7593840851324420633
www.dongchedi.com/article/7593836770471625241
www.dongchedi.com/article/7593833071250948670
www.dongchedi.com/article/7593831056378790462
www.dongchedi.com/article/7593830088668906009
www.dongchedi.com/article/7593830311700988478
www.dongchedi.com/article/7593831918585004568
www.dongchedi.com/article/7593830135552311833
www.dongchedi.com/article/7593829391919170073
www.dongchedi.com/article/7593830256495542808
www.dongchedi.com/article/7593828787180388888
www.dongchedi.com/article/7593829247186223678
www.dongchedi.com/article/7593823622788186648
www.dongchedi.com/article/7593824480053559832
www.dongchedi.com/article/7593824197931975192
www.dongchedi.com/article/7593822824964358718
www.dongchedi.com/article/7593821230626013720
www.dongchedi.com/article/7593797606556910104
www.dongchedi.com/article/7593862164004209176
www.dongchedi.com/article/7593859135339479576
www.dongchedi.com/article/7593858557611803160
www.dongchedi.com/article/7593857292345590334
www.dongchedi.com/article/7593856001158332953
www.dongchedi.com/article/7593854938854375961
www.dongchedi.com/article/7593853320968585790
www.dongchedi.com/article/7593850791878951449
www.dongchedi.com/article/7593849985222230553
www.dongchedi.com/article/7593850714343424536
www.dongchedi.com/article/7593845458314297880
www.dongchedi.com/article/7593845882341376574
www.dongchedi.com/article/7593845435790836249
www.dongchedi.com/article/7593844887007593022
www.dongchedi.com/article/7593844459406852670
www.dongchedi.com/article/7593842809959285273
www.dongchedi.com/article/7593843735868834329
www.dongchedi.com/article/7593843853976027710
www.dongchedi.com/article/7593840934157566489
www.dongchedi.com/article/7593842500343906841
www.dongchedi.com/article/7593843018189292094
www.dongchedi.com/article/7593841511696155198
www.dongchedi.com/article/7593840009971548696
www.dongchedi.com/article/7593832297598960152
www.dongchedi.com/article/7593832634816578073

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/30 9:20:20

单调栈学习C++

【题目描述】 对于给定的一个temperatures数组&#xff0c;每个元素表示当天的温度。对于每天的温度&#xff0c;求出下一次更高的温度出现在几天后。 【算法】单调栈 我们可以维护一个栈结构&#xff0c;先将数组的首元素入栈&#xff0c;然后开始遍历这个数组&#xff0c;…

作者头像 李华
网站建设 2026/3/31 7:36:17

lvgl移植全面讲解:输入设备与显示接口适配入门

从零搞定LVGL移植&#xff1a;显示与触控底层适配实战指南你有没有遇到过这样的场景&#xff1f;精心设计的UI在模拟器里丝滑流畅&#xff0c;结果一烧进开发板——屏幕黑屏、触摸错位、点击毫无反应。调试几天还找不到原因&#xff0c;最后只能怀疑人生。别急&#xff0c;这几…

作者头像 李华
网站建设 2026/3/27 18:26:12

Jellyfin豆瓣插件配置全流程解析

Jellyfin豆瓣插件配置全流程解析 【免费下载链接】jellyfin-plugin-douban Douban metadata provider for Jellyfin 项目地址: https://gitcode.com/gh_mirrors/je/jellyfin-plugin-douban 在构建个人媒体服务器的过程中&#xff0c;Jellyfin作为优秀的开源媒体管理系统…

作者头像 李华
网站建设 2026/3/27 21:01:39

zotero-style插件完整指南:从零开始掌握智能文献管理

zotero-style插件完整指南&#xff1a;从零开始掌握智能文献管理 【免费下载链接】zotero-style zotero-style - 一个 Zotero 插件&#xff0c;提供了一系列功能来增强 Zotero 的用户体验&#xff0c;如阅读进度可视化和标签管理&#xff0c;适合研究人员和学者。 项目地址: …

作者头像 李华
网站建设 2026/3/16 2:54:47

MsgViewer终极指南:免费跨平台MSG文件查看器解决方案

MsgViewer终极指南&#xff1a;免费跨平台MSG文件查看器解决方案 【免费下载链接】MsgViewer MsgViewer is email-viewer utility for .msg e-mail messages, implemented in pure Java. MsgViewer works on Windows/Linux/Mac Platforms. Also provides a java api to read ma…

作者头像 李华
网站建设 2026/3/31 4:29:43

RimWorld终极模组管理器:一键智能排序与冲突检测完整指南

RimWorld终极模组管理器&#xff1a;一键智能排序与冲突检测完整指南 【免费下载链接】RimSort 项目地址: https://gitcode.com/gh_mirrors/ri/RimSort RimSort是一款专为RimWorld玩家设计的开源模组管理器&#xff0c;提供智能排序、冲突检测和Steam集成等强大功能。这…

作者头像 李华