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 ...
随机推荐
- young tableaus
Young tableaus 这是 Introduction_to_algorithms一个 路学校运动, 我也难倒,互联网没有找到现有的应答. 今天 python 代码贴,供你参考. #! /us ...
- 使用Spring的JAVA Mail支持简化邮件发送(转)
闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是Mail ...
- Linux iptables简单配置
#!/bin/sh#modprobe ipt_MASQUERADEmodprobe ip_conntrack_ftpmodprobe ip_nat_ftpiptables -Fiptables -t ...
- 蓝桥杯软件学院webserver,android
- veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响
veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响 续接veridata实验系列 上篇:"veridata实验举例(5)改动主键上的列值.update ...
- WPF界面设计技巧(9)—使用UI自动化布局
原文:WPF界面设计技巧(9)-使用UI自动化布局 最近一直没时间更新这系列文章,因为我一直在埋头编写我的第一个WPF应用程序:MailMail 今天开始编写附属的加密/解密工具,对UI自动化布局有些 ...
- MySQL 触发器例子(两张表同步增加和删除)
以下两个例子来自:http://www.cnblogs.com/nicholas_f/archive/2009/09/22/1572050.html实测有效,但是原帖的分隔符不正确,因此稍作修改.其中 ...
- Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)
Application Design Concepts and Principles Identify the effects of an object-oriented approach to sy ...
- POJ训练计划2418_Hardwood Species(Trie树)
解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...
- asp.net不能调试,配置一切正常
Asp.net发展中遇到的一个奇怪的想象:一个简单的button事件,不能调试.即使webconfig里面 "debug=true". 开发环境:win7+VS2005+IE8. ...