【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 ...
随机推荐
- 使用FusionCharts创建可更新数据的JavaScript图表
先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...
- Linux uart程序
我用的是jetson tx1 开发板 都是linux系统出了串口文件可能不同其他的没有什么不同都能用. 我安装的是qt5 新建一个none qt c工程,用c 语言开发 期间调试了两天结果还是发送和 ...
- SAP成都研究院马洪波:提升学习力,增强竞争力,收获一生乐趣
马洪波是SAP成都研究院CEC开发团队三大巨头之一.关于他的背景介绍,参考我以前的公众号文章:SAP成都研究院CEC团队三巨头之一:M君的文章预告. 其实早在2007年,互联网上已经有介绍马洪波的文章 ...
- 访问mongo数据库报错
It looks like you are trying to access MongoDB over HTTP on the native driver port. 出错原因: 1.没有安装mong ...
- UVA 10375 Choose and divide(大数的表示)
紫上给得比较奇怪,其实没有必要用唯一分解定理.我觉得这道题用唯一分解只是为了表示大数. 但是分解得到的幂,累乘的时候如果顺序很奇怪也可能溢出.其实直接边乘边除就好了.因为答案保证不会溢出, 设定一个精 ...
- POJ 1742 Coins(多重背包,优化)
<挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...
- java 字符串中是否有数字
http://www.cnblogs.com/zhangj95/p/4198822.html http://www.cnblogs.com/sunzn/archive/2013/07/12/31865 ...
- AngularJS 字符串
AngularJS字符串就像是JavaScript字符串 <!DOCTYPE html><html><head><meta http-equiv=" ...
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...
- qsort()和bsearch()
qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); Sort ...