The problem:
Given an array of non-ifreturntruereturnfalse .
My Analysis:
This question is very easy! Just need to understand clearly about the underlying invariant forreaching each element in the Array. The key Idea:max_reach indicates the maximum position that could is reached now. At each position, we Dofollowing things:1. TestifThe current element could is reached by the previous elements (Max_reach).if(Max_reach <i) {return false;}2.ifThe current element could is reached, we checkifWe need to update the Max_reach.iff Max_reach< i +A[i], it indicates the Max_reach element could be Further.iff Max_reach>= i + a[i], we need to DoNothing.max_reach= Math.max (Max_reach, i + a[i]);
My Solution:
Public classSolution { Public BooleanCanjump (int[] A) {if(A = =NULL|| A.length = = 1) return true; intMax_reach = 0; for(inti = 0; i < a.length; i++) { if(Max_reach <i) {return false; } Else{Max_reach= Math.max (Max_reach, i +A[i]); } } return true; }}
Problem 2:
Given an array of non-= [2,3,1,1,42. (Jump 1 Step from index 0 to 1 and then 3 steps to the last index.)
My Analysis:
The idea behind Thisproblem is Easy.key idea:use a max_reach[] array to record the maximum position the elements (before and include Elem ent i) could reach.max_reach[i]: Indicate how far we can reach from the elemenets (before and include element I). for(inti = 0; i < a.length; i++) { if(Pre_max <i)return-1; if(A[i] + i >Pre_max) {Max_reach[i]= A[i] +i; Pre_max= A[i] +i; } Else{Max_reach[i]=Pre_max; } }1. At each position, we have a reach range: [cur, max_reach[i]]. In the range, we would as to step farest (to quickly reach the last element). The range ' s farest reach is stored at Max_reach[max_reach[i]], cause we only record the farest position we could reach at Max_reach Array. A = [2, 3, 1, 1, 4] Max_reach = [2, 4, 4, 4, 8]at a[0], the reach range is [0, 2]. The farest position we could reach through the range is record at max_reach[2]. Note the invariant, it is very very interesting!We make our decison not based on the Farest position (range) of the current element could reach, but on the Farest Positio N (range) The current range could reach!How to get the range' s information? We use max_reach[] arrays, it records the cultimative information in the array. !!!Range computation.2. There is a little pitfall we should take care, that's we only need to reach the last element! Don ' t continue the jump out of the array. while(Temp_reach < A.length-1) {Temp_reach=Max_reach[temp_reach]; Count++;}
My Solution:
Public classSolution { Public intJumpint[] A) {if(A = =NULL|| A.length = = 0) return-1; int[] Max_reach =New int[A.length]; intPre_max = 0; intCount = 0; intTemp_reach; for(inti = 0; i < a.length; i++) { if(Pre_max < i)//If we could not return-1; if(A[i] + i >Pre_max) {Max_reach[i]= A[i] +i; Pre_max= A[i] +i; } Else{Max_reach[i]=Pre_max; }} Temp_reach= 0; while(Temp_reach < A.length-1) {Temp_reach= Max_reach[temp_reach];//except for last element, there is no possible:max_reach[temp_reach] = Temp_reach <we has a detection ahead!>
Count + +; } returncount; }}
[Leetcode#55, 45] Jump game, jump game II