Leetcode - Jump Game Two
和Jump Game几乎相同的想法,他们是DP。关键是使用数组maxNumbers[k]储存的地方k步骤的话。序列号的最远范围,注阵maxNumbers[]它递增。
class Solution {
public:
const int MAXVALUE = 1 << 30;
int findMinStepToIndex(int maxNumbers[],int maxSteps,int index)
{
if (index == 0)
return 0;
int left = 1;
int right = maxSteps;
while (left < right)
{
int m = (left + right) / 2;
if (maxNumbers[m] < index)
{
left = m + 1;
}
else if (maxNumbers[m] > index)
{
if (maxNumbers[m - 1] < index)
return m;
else if (maxNumbers[m - 1] == index)
return m - 1;
else
right = m - 1;
}
else
{
return m;
}
}
return (right + left) / 2;
}
int jump(int A[], int n) {
int* maxNumbers = new int[n];//mark the max number that steps i can walk.
int maxIndex = 0;
int maxNumber = 0;//the max index we can walk to.
maxNumbers[0] = 0;
for (int i = 1; i < n; i++){
maxNumbers[i] = 0;
}
int maxSteps = 0;
for (int i = 0; i < n - 1; i++){
if (maxNumber < i + A[i])
{
int cMinStep = findMinStepToIndex(maxNumbers, maxSteps, i);
maxNumbers[cMinStep + 1] = i + A[i];
maxNumber = i + A[i];
maxSteps = cMinStep + 1;
if (maxNumber >= n - 1)
return maxSteps;
}
}
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
Leetcode - Jump Game Two的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- html5css3杂记
最新版本号的safari.chrome.firefox以及opera支持某些html5特性.ie9将支持某些html5特性. html5提供了展现视频的标准<video>支持ogg及mpe ...
- hdu5124(树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:有n条线段,求被覆盖到次数最多的点的次数 分析: 1.可以转化成求前缀和最大的问题:将区间 ...
- Android Studio 入门(转)
本文适用于从Eclipse转AndroidStudio的开发者 最近打算写一个系列的android初级开发教程,预计40篇以上的文章,结合我实际工作中的经验,写一些工作中经常用到的技术,让初学者可以少 ...
- poj3252(数位dp)
题目连接:http://poj.org/problem?id=3252 题意:拆成2进制,在记录0和1的个数 求区间[a,b]中,满足传化成2进制后,0的个数>=1的个数的数字的个数... 分析 ...
- poj2486(树形dp)
题目链接:http://poj.org/problem?id=2486 题意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走m步,最多能遍历到的权值. 分析:非常不错的树形d ...
- finger用户名、主目录、停滞时间、登录时间
finger yum install finger 1.作用 finger用来查询一台主机上的登录账号的信息,通常会显示用户名.主目录.停滞时间.登录时间.登录Shell等信息,使用权限为 ...
- java性能缓慢
虚拟帝国上面有很多营销软件是JAVA开发的!创业公司通常选择开源技术减少项目管理费用. 除了使用Java编程语言,创业公司也可以利用Java开发工具包的好处(JDK),Java运行时环境(JRE)和J ...
- App域名劫持之DNS高可用 - 开源版HttpDNS方案详解(转)
http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=209805123&idx=1&sn=ced8d67c3e2cc3 ...
- Java的wait(), notify()和notifyAll()使用心得(转)
本篇文章是对java的 wait(),notify(),notifyAll()进行了详细的分析介绍,需要的朋友参考下wait(),notify()和notifyAll()都是java.lang.Obj ...
- leetcode先刷_Binary Tree Level Order Traversal II
非常easy标题,在后面,我不认为它不是那么简单的回答更多的.我们将编写,没有人啊. 预购在基层上,加上节省每一层,加上从下往上的输出,是一家vector而一个stack那么问题,没有他,但另一方面- ...