Jump Game II
来源:互联网 发布:微店 淘宝客 编辑:程序博客网 时间:2024/05/16 11:14
class Solution {public: int jump(int A[], int n) {//table[i] 为 i jump to n-1 的最少步数,超时int *table=new int[n];memset(table,-1,n*sizeof(int));table[n-1]=0; return _jump(A,n,0,table); }int _jump(int A[],int n,int i,int *table){if(table[i]==-1){if(i+A[i]>=n-1){table[i]=1;}else{int minVal=INT_MAX;for(int j=i+1;j<=i+A[i] && j<(n-1);j++){minVal=min(minVal,_jump(A,n,j,table));}table[i]=minVal+1;}}return table[i];}};
class Solution {public: int jump(int A[], int n) { if(n==1) return 0; int pre=0,cur=A[0],next=A[0];/*pre:上一个可以到达区域的右边界cur:由[0:pre]Jump可以到达区域的右边界next:由[0:cur]Jump可以到达区域的右边界*/if(cur>=n-1){ return 1;}int step=0;while(pre<cur){ step++;for(int i=pre+1;i<=cur;i++){next=max(next,i+A[i]);if(next>=n-1){return step+1;}}pre=cur;cur=next;next=cur;}return -1; }};
0 0
- Jump Game &Jump Game II
- Jump Game/Jump Game II
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- 追加写入txt
- 会场安排问题
- oracle错误:1067进程意外终止
- Java 多线程 死锁 Java编程思想读书笔记
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
- Jump Game II
- 黑马程序员_内省和JavaBean笔记
- wegergerhge
- 斯蒂芬二个如果
- 电话地方规划法规和分
- vs2013 Ultimate激活码
- 是豆腐干豆腐干豆腐干豆腐干的
- 的分好风光好风光
- 双方各地方豆腐干规划