problem:
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.)
anaslysis:
This is also a dynamic problem. It reminds you there could is good and bad dynamic algorithm. Once feel like something is repeatedly done. Yous shouldTrya better solution to solve it. Solution1: This idea is instant, and suitable formy thinking habit, but it's really not efficient. Basic Idea:use A array to record the minimum steps required to reach a element. Then we use the following function to update on it.------------------------------------------------------------- for(intj = 1; J <= Nums[i]; J + +) { if(I+j <Len) {Nums[i+J] = math.min (nums[i]+1, nums[i+J]); }}-------------------------------------------------------------
Solution 1:
Public classSolution { Public intJumpint[] nums) { if(Nums = =NULL) Throw NewIllegalArgumentException ("Nums is null!"); intLen =nums.length; if(Len <= 1) return0; int[] min =New int[Len]; Arrays.fill (min, integer.max_value); min[0] = 0; for(inti = 0; i < Len; i++) { for(intj = 1; J <= Nums[i]; J + +) { if(I+j <Len) {Min[i+J] = math.min (min[i]+1, min[i+J]); } } } returnMin[len-1]; }}
UpgradedAnalysis:
Time Limit Exceededeven The idea of above solution was right, but there was repeates in computing. We Know:iff i< J, the steps to reach I must no greater than J. Step[i] <=Step[j]reason:once we could reach Step[j], we must be able to reach Step[i] (just fewer steps) ThusifWe have already reached step[j] from step[i], we should we test iff we could use step[i+1] to reach Step[j]. The only thing we should take care, IFF Step[i+1] could help us to reach further elements!The update solution:we should the window for Thisproblem, latest reach:the maximum element we could reach from previous window' s elements. (Previous step)reach:the maimum Element we could reach from the current window. (Current step) {[1]}, {[2]}, {[1] [3]}, {[2] [4] [5]} 0 1 2 3 4 5 6For example:at Step0, our latest reach was 0 (the current element), after scan the [1] element in the window, the reach becomes 1, which means We can reach element nums[1]. We Keep the above invariant to update our next reach windowThrowLatest Reach window (current) Reach= Math.max (Reach, Nums[i] +i); But reach is forNext window, once we enter the elements at reach window, it means we had to use one step to achieve it. Thus we have following code for Thispurpose. for(inti = 0; I <= reach && i < Len; i++) { if(I >Last_reach) {Step++; Last_reach=reach; }...} Note:we Count the step, only if we need to enter the window. (Reach Nums[i]) Also note:the checking codition forThe forLoopifI exceed the maximum range of reach, it means there is no path. We should stop at here.
Solution 2:
Public classSolution { Public intJumpint[] nums) { if(Nums = =NULL) Throw NewIllegalArgumentException ("Nums is null!"); intLen =nums.length; if(Len <= 1) return0; intLast_reach = 0; intStep = 0; intReach = 0; for(inti = 0; I <= reach && i < Len; i++) { if(I >Last_reach) {Step++; Last_reach=reach; } Reach= Math.max (Reach, Nums[i] +i); } return(Reach >= nums.length-1)? step:0; }}
[LEETCODE#45] Jump Game II