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 ...
随机推荐
- 2014-5-22 java.lang.OutOfMemoryError: Java heap space的一次诊断
收到消息某系统一个节点因为内存溢出而宕机.系统的中间件是weblogic.数据库的oracle. 1. 先用IBM HeapAnalyzer分析内存溢出时的dump文件,找到占用内存最多的请求,然后 ...
- MSF连环攻击实验
MSF连续攻击实验 一.实验拓扑 二.实验环境 Windows XP BT 5 32位 三.实验原理 通过扫描 XP主机,利用扫描出的漏洞建立 TCP会话,通过进程的提权,进一步获取目标机的控制权限 ...
- wx_sample.php
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin&quo ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- TMG 2010 VPN配置
微软的ISA 到2006以后就叫TMG了,上周在公司的服务器上安装测试了下,虽然增加了很多功能,但是主要功能上和ISA 2004差不多,最近在部署L2TP VPN,由于防火墙带的远程访问VPN为纯的L ...
- 【翻译】我钟爱的Visual Studio前端开发工具/扩展
原文:[翻译]我钟爱的Visual Studio前端开发工具/扩展 怎么样让Visual Studio更好地编写HTML5, CSS3, JavaScript, jQuery,换句话说就是如何更好地做 ...
- JS数组追加数组採用push.apply的坑
JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这样的自以为非常酷的,不须要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个非常大的 ...
- WebService之Soap头验证入门
1.新建一个类,如"AuthHeaderUser",继承于"System.Web.Services.Protocols.SoapHeader"类 2.新建Web ...
- (转载)浅析error LNK2001: unresolved external symbol "public: __thisc...
学习VC++时经常会遇到链接错误LNK2001,该错误非常讨厌,因为对于 编程者来说,最好改的错误莫过于编译错误,而一般说来发生连接错误时, 编译都已通过.产生连接错误的原因非常多 ...
- WPF实现无窗体鼠标跟随
原文:WPF实现无窗体鼠标跟随 上次的弹力模拟动画实现后,我觉得可以把这个弄得更好玩一些,我们可以让小球实时跟随着鼠标,并且还可以让窗口完全消失,让小球在桌面上飞来飞去. 这只需要一些简单的修改就可以 ...