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 ...
随机推荐
- CSS实现输入框的高亮效果-------Day50
又到周末了,这一天天过的真快,明天应该回老家了.不知道会不会有机会进行编写.尽量争取吧,实在不想就这样间断.假设说从前会一天天无聊到爆,那如今自己应该是一天天忙的要死,欠缺了太多东西,那些浪费的时间可 ...
- 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)
然后上传到该项目SVN仓库上,例如,下面的错误再次发生再拉到本地编译 ld: library not found for -lxxxxxxxxxxxx clang: error: linker com ...
- 从尾到头打印链表--《剑指offer》
题目:非常easy,就是题目,将链表从尾到头打印出来. 可能我们首先想到的是将链表进行遍历,将之前的訪问的数据进行保存,最后进行反向输出,但是保存数据的空间是个问题:或者是我们将整个链表进行反向操作, ...
- 将svnkit转成dlls时的问题
未处理 System.TypeInitializationException Message="“org.tmatesoft.svn.core.internal.wc.DefaultSVNO ...
- java參数传递方式问题
java的參数传递方式到底是值传递还是引用传递,这一直是一个争论不休的问题,一直以来没有形成统一意见. 在这里,我也仅仅是说一说个人见解,不保证是对的,全当是抛砖引玉. 首先我的观点是java採用的是 ...
- HDU 3304 Interesting Yang Yui Triangle lucas定理
输入p n 求杨辉三角的第n+1行不能被p整除的数有多少个 Lucas定理: A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0] ...
- WebSocket API
WebSocket API 这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些 ...
- Android - 通过Intent启动Activity
通过Intent启动Activity 本文地址: http://blog.csdn.net/caroline_wendy 为了动态关联Activity界面,使用Intent启动.能够灵活绑定. 在In ...
- Kruskal(克鲁斯卡尔)
设有一个有n个顶点的连通网N={V,E},最初先构造一个只有n个顶点, 没有边的非 连通图 T={V, E}, 图中每个顶点自成一个连通分量. 当在E中选到一条具有最小权值的边时,若该边的两个顶点落在 ...
- HDU 1661 Assigments 贪心法题解
Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each t ...