Jump Game (middle)
Given an array of non-negative integers, you is initially positioned at the first index of the array.
Each element of the array represents your maximum jump length is at that position.
Determine if you is able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
Idea: Start not responding, from the back to the record can reach the result time-out
And then it came back. The maximum position that can be reached from the previous back record is >= n-1 on the line.
bool canjump (intint n) { int0; for (int0; i < n; i++) { if break;//If the current maximum position cannot be reached I description change unreachable = (i + a[i] > Maxdis)? i + A[i]: Maxdis; } return 1 ;}
Simplified post Code
bool canjump (intint n) { int0; for (int0; i < n && maxdis >= i; i++) { = (i + a[i] > Maxdis)? i + a[i]: Maxdis; } return 1 ;}
Jump Game II didn't make it.
Given an array of non-negative integers, you is initially positioned at the first index of the array.
Each element of the array represents your maximum jump length is at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is 2
. (Jump 1
Step from index 0 to 1 and then steps to the last 3
index.)
Idea: I used dynamic programming O (N2) timed out
//timed outintJumpintA[],intN) {int* DP = (int*)malloc(n *sizeof(int)); Dp[n-1] =0; for(inti = n-2; I >=0; i--) {Dp[i]=N; for(intj = i +1; J <= i + a[i] && J < N; J + +) {Dp[i]= (Dp[i] < Dp[j] +1) ? Dp[i]: Dp[j] +1; } } returndp[0];}
Following the code and ideas of the great gods, not yet seen
intJumpintA[],intN) {if(n = =0){ return 0; } intMaxreachpos = a[0]; intCurmaxreachpos = a[0]; intCurstep =1; for(inti =1; I <= min (n, maxreachpos); i++) {Curmaxreachpos= Max (Curmaxreachpos, i +A[i]); if(i = = N-1){ returnCurstep; } if(i = =Maxreachpos) {Maxreachpos=Curmaxreachpos; Curstep++; } } return 0;}
The variable maxreachpos indicates the farthest reachable position and the variable curmaxreachpos indicates the current F Arthest reachable position.
At the very beginning, both Maxreachpos and Curmaxreachpos is equal to a[0].
In the as loop, we keep updating curmaxreachpos while I <= maxreachpos. However, if (i = = n-1), we return Curstep, which is the minimum step. If I reaches the maxreachpos, we update maxreachpos with Curmaxreachpos and increment curstep by one.
Finally, if we can ' t reach the end point, just return 0.
BFS practices
I try to change this problem to a BFS problem, where nodes with level I is all the nodes so can be reached on i-1th jump. For example. 2 3 1 1 4, is 2| | 3 1| | 1 4 | |
Clearly, the minimum jump of 4 are 2 since 4 is in Level 3. My AC code.
intJumpintA[],intN) {if(n<2)return 0; intLevel=0, currentmax=0, i=0, nextmax=0; while(currentmax-i+1>0){//nodes Count of current level>0level++; for(; i<=currentmax;i++) {//traverse current level, and update the Max reach of next levelNextmax=max (nextmax,a[i]+i); if(nextmax>=n-1)returnLevel//if last element was in level+1 and then the Min jump=level} Currentmax=Nextmax; } return 0; }
"Leetcode" Jump Game I & II (Hard)