【Jump Game II 】cpp
题目:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
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.)
代码:
class Solution {
public:
int jump(vector<int>& nums) {
int max_jump=, next_max_jump=, min_step=;
for ( int i = ; i<=max_jump; ++i )
{
if ( max_jump>=nums.size()- ) break;
if ( max_jump < i+nums[i] ) next_max_jump = std::max(next_max_jump, i+nums[i]);
if ( i==max_jump )
{
max_jump = next_max_jump;
min_step++;
}
}
return min_step;
}
};
tips:
参考Jump Game的Greedy思路。
这道题要求求出所有可能到达路径中的最短步长,为了保持O(n)的解法继续用Greedy。
这道题的核心在于贪心维护两个变量:
max_jump:记录上一次跳跃能跳到最大的下标位置
next_max_jump:记录遍历所有max_jump之前的元素后,下一次可能跳到的最大下标位置
举例说明如下:
原始数组如右边所示:{7,0,9,6,9,6,1,7,9,0,1,2,9,0,3}
初始:max_jump=0 next_max_jump=0 min_step=1
i=0:next_max_jump=7 更新max_jump=7 更新min_step=1
i=1: 各个值不变
i=2: i+nums[i]=2+9=11>7 更新next_max_jump=11
i=3:i+nums[i]=3+6=9<11 不做更新
i=4: i+nums[i]=4+9=13>11 更新max_jump=13
...
i=7:i+nums[i]=7+7=14 >= nums.size()-1 返回min_step=2
完毕
==========================================
第二次过这道题,不太顺,大体复习了下思路。
class Solution {
public:
int jump(vector<int>& nums) {
if ( nums.size()== ) return ;
int ret = ;
int nextJump = ;
int maxLength = ;
for ( int i=; i<=maxLength; ++i )
{
if ( maxLength>=nums.size()- ) break;
nextJump = std::max(i+nums[i], nextJump);
if ( i==maxLength )
{
maxLength = nextJump;
ret++;
}
}
return ret;
}
};
==========================================
第三次过这道题,把代码改了一行,但是整体结构清晰了不少。
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size()<) return ;
int steps = ;
int local = ;
int next = local;
for ( int i=; i<=local; ++i )
{
next = max(next, i+nums[i]);
if ( i==local )
{
steps++;
local = next;
if ( local>=nums.size()- ) return steps;
}
}
return ;
}
};
next只负责看下一跳能够到哪。
什么时候更新local了,再判断能否跳到尾部。
【Jump Game II 】cpp的更多相关文章
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【N-Quens II】cpp
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
随机推荐
- winform 自适应屏幕分辨率具体操作和注意事项
第一步:先借助一个类文件 AutoSizeFormClass.cs class AutoSizeFormClass { public struct controlRect { public int L ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:6.技术简介之Protobuf
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 protocolbuffer(以下简称Protobuf)是google 的一种数据交换的格式,它独立于语言,独立于平 ...
- cms-登陆
先介绍下登陆的思路: 1.在登陆页面首先前端验证用户名和密码是否正确,如果验证通过,则ajax的方式向后台提交数据. 2.在controller层,将得到的用户名名和密码封装进shiro的token, ...
- 【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)
点此看题面 大致题意: 有\(n\)个弹力装置,当到达第\(i\)个装置时,会被弹到第\(i+k_i\)个装置,若不存在第\(i+k_i\)个装置,就会被弹飞.有两种操作,一种操作是将\(k_x\)改 ...
- Java 集合框架_上
集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的 ...
- 五、react中父子组件间如何传值
1.父组件向子组件传递数据:父组件绑定属性值传给子组件,子组件通过this.props()接受. 2.子组件向父组件传递数据:子组件绑定一个方法,方法中通过this.props.父组件方法名(参数)传 ...
- matlplotlib 为折线图填充渐变颜色
概要 本篇记录绘图时填充颜色时的一些常用设置,主要用到了 imshow,fill 函数. 填充图实例 填充的效果图如下: 图 1:渐变色效果图 可根据下方给出的代码进行自定义. #!/us ...
- Ajax的学习记录
Ajax学习笔记 1.同步与异步同步:客户端发送请求到服务端,当服务器返回响应之前,客户端都处于等待卡死状态异步:客户端发送请求到服务端,当服务器返回响应之前,客户端可以随意做其他事情,不会卡死 2. ...
- Spring+ ApplicationListener
有时候 需要在容器初始化完成后,加载些 代码字典或不常变的信息 放入缓存之类的,这里使用spring 初始化bean,并实例化 1.创建一个ApplicationListener类 import o ...
- linux常用指令学习记录
前言 本文主要为学习贴,用来记录一些 linux上的常用指令 以供参考. 文件内容查看 cat 从上往下阅读文件内容 cat [-AbEnTv] ${FILE_NAME) cat -n /etc/is ...