Title:
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
.
Ideas:
Using the greedy algorithm, use Maxstep to record the farthest distance of the current position jump, update maxstep = max (a[i],maxstep), each step forward, maxstep--
classSolution { Public: BOOLCanjump (intA[],intN) {if(n = =0|| n = =1) return true; intMaxstep = a[0]; for(inti =1; I < n; i++){ if(0==maxstep)return false; Maxstep--; Maxstep=Max (maxstep,a[i]); if(i + maxstep >= N-1) return true; } }};
Jump Game2
Title
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 1: Use dynamic planning to do it, but timeout
intJump (vector<int>&nums) { intn =nums.size (); Vector<int>result (N,int_max); result[0] =0; for(inti =0; I < nums.size (); i++){ for(intj = i+1; J <= i+ Nums[i]; J + +){ if(J >=nums.size ()) Break; RESULT[J]= Min (result[j],result[i]+1); } } returnresult[n-1]; }
Idea 2: Daniel writes the scan again. I think about it, scan it again and have some similarities with dynamic planning. In dynamic planning, we need to update the number of hops for each of the other points within the range of his jump. So sweep the face over the idea, is to use two variable last,cur to record, last is recorded before the step can jump the farthest distance, cur is recorded under the current can reach the furthest distance. Update last is in the current I more than last, then indicates that has been breached before the sphere of influence, need to update, with
Http://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html examples to illustrate
For example, [2,3,1,1,4] in our topic. The initial state is this: cur indicates the farthest possible coverage, in red. Last indicates where it has been covered, denoted by an arrow. All of them are referred to on the first element.
Next, the first element tells Cur that the furthest we can walk is 2 steps. So:
In the next loop, I points to 1 (element 3 in the figure), and I find that, oh, I is less than the last to reach the range, so the update is (to say, into a new sphere of influence), the number of steps RET plus 1. To update the cur. Because the furthest distances were found.
Next, I go ahead and find I within the current sphere of influence without having to update last and step ret. Update cur.
I go ahead and find out more than the current sphere of influence, updating the last and the steps. Cur is already the biggest.
Finally, I to the last element. Still within the sphere of influence, the traversal is complete and returns to Ret.
/** We use ' last ' to keep track of the maximum distance that have been reached * by using the minimum steps "ret", where As "Curr" is the maximum distance * This can be reached by using "ret+1" steps. Thus, * Curr = max (i+a[i]) where 0 <= i <= last. */classSolution { Public: intJumpintA[],intN) {intRET =0; intLast =0; intCurr =0; for(inti =0; I < n; ++i) {if(I >Last ) { Last=Curr; ++ret; } Curr= Max (Curr, i+A[i]); } returnret; }};
Leetcode:jumpgame 1 and 2