和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的更多相关文章

  1. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [leetcode]Jump Game @ Python

    原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...

  5. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. LeetCode: Jump Game Total 解题报告

    Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...

  7. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

  8. [LeetCode] Jump Game II 贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  10. Leetcode jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. NOJ1184 失落的邮票 哈希表

    意甲冠军 我们共收集N邮票.现在失去了2张,剩下N-2张-..原集邮收集了所有对.因此,找到什么两枚邮票是一个.它们输出. (确定缺少邮票是不一样的) 思路 由于编号比較大,能够用hash表压缩成数组 ...

  2. Codeforces 452A Eevee

    #include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...

  3. ios UITableView 相关

    1.tableView 实现的方法 无分组的cell #pragma mark - Table view data source - (NSInteger)tableView:(UITableView ...

  4. Spring实战笔记2---Bean的装配

    创建应用对象之间协作关系的行为通常成为装配,该篇的主要内容有两个,一个Spring装配Bean的几种方式以及Spring表达式,事实上这两者是分不开的,在Spring中,对象无需自己负责查找或者创建与 ...

  5. Driver 初始化顺序

    Linux系统使用两种方式去加载系统中的模块:动态和静态. 静态加载:将所有模块的程序编译到Linux内核中,由do_initcall函数加载 核心进程(/init/main.c)kernel_ini ...

  6. Extjs4.10Model模型具体解释

    一.创建Model模型 Extjs4.10提供了两种方法来创建Model模型,也就是创建类 方法一: Ext.define('person',{              extend:'Ext.da ...

  7. C语言sizeofkeyword

    说明: ******C语言sizeof是keyword.是一个操作符.它不是一个函数.用于计算可变.或内存数据字节数占用类型. ******sizeof有三种不同的方式: ***sizeof(变量名) ...

  8. CCNA CCNP CCIE所有实验名称完整版

    实验1:通过Console端口访问Cisco路由器  实验2:通过Telnet访问Cisco路由器  实验3:配置终端服务器  实验4:通过浏览器访问路由器  实验5:模式切换.上下文帮助及查看有关信 ...

  9. 【Android】应用启动画面

    几乎所有的Android应用程序都会有一个启动画面,展示自己的LOGO,本版信息,或者更人性化一点的,在很长的加载信息中,变换一些显示的文字等,让无聊的等待时间添加点调味剂. 具体实现来说,应该创建一 ...

  10. sed中求公共前缀

    string1="test toast" string2="test test" printf "%s\n%s\n" "$stri ...