Topic:
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.)
The problem is self-made.
The method of dynamic programming is considered first. Defines an array distance[n] (n=a.length) Distance[i] that represents the minimum number of steps required to move from a[0] to a[i].
When initialized, all Distance[i] are postive limit. Indicates that none of them can be reached.
Then the element is added in turn, the first cycle starts from a[0], which indicates the minimum number of steps from a[0] to reach each point.
The second round starts from a[1], indicating the minimum number of steps to arrive at each point from a[1].
。。。。 And so on
Then its state transfer equation is
if ((j-i) <distance[i]) // Whether you can reach J from I if (Distance[i]+1<distance[j]) // is this route better than the current one ? Distance[j]=distance[i]+1
however timeout.
Considering, there is greedy in the tag of the problem. Then start thinking about greedy methods.
This topic, as long as the value of a[i] is greater than its stride, you can jump to. Therefore, just consider this condition.
The element position is no longer considered, but the number of steps is considered. That is, the furthest you can jump to, the furthest you can jump in two steps, and so on, until you can jump to the end.
Take two steps as an example
From a[0] to a[i]. When 0<k<i, A[k] can be reached in one step
Therefore, when the case is 2 steps, you can start from any point in the 1-i and calculate the farthest possible place, assuming J.
When the situation is 3 steps, just calculate the maximum distance that can be reached from I,I+1,I+2....J.
When the maximum distance exceeds the length of array A, the number of steps is returned.
The code is given below
ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.util.Scanner; Public classSolution {/** * @paramargs*/ Public intJumpint[] nums) { intEnd=nums[0]; intPath=1; intLastpoint=0; intNext=nums[0]; if(nums.length==1) return0; while(true) { if(next>=nums.length-1) returnpath; Next=Findmaxmid (nums,lastpoint,end); Path++; Lastpoint=end; End=Next; //System.out.println (lastpoint+ "*" +end); } } Public intFindmaxmid (int[]nums,intLastpoint,intend) { intMax=-1; intMaxindex=-1; for(inti=lastpoint;i<=end;i++) { if(max<nums[i]+i) {max=nums[i]+i; Maxindex=i; } } returnMax; } Public Static voidPrintint[]nums] {String result="{"; for(inti:nums) {Result+=i+ ","; } System.out.println (Result+"}"); } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub int[]a=New int[10000]; for(inti=0;i<10000;i++) {A[i]=1; } int[]b={2,3,1,1,4}; int[]c=New int[25002]; for(inti=25000;i>=1;i--) {c[25000-i]=i; } c[25000]=1; c[25001]=0; //new Solution2 (). print (c);Scanner scanner=NULL; Try{Scanner=NewScanner (NewFile ("D://data.txt")); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } int[] d=New int[30000]; intI=0; while(Scanner.hasnext ()) {String all=Scanner.next (); for(String as:all.split (",") ) {D[i]=Integer.parseint (AS); I++; } } for(intj=0;j<i;j++) {C[j]=D[j]; } print (c); System.out.println (Newsolution (). Jump (c)); }}
Leetcode Jump Game II