Why we need the code: maxJump = lastmax; it seems meaningless after lastmax = maxJump; but it will TLE without the code.


  • 0
    G
    public int jump(int[] nums) {
          int maxJump = nums[0];
    	  int lastmax = nums[0];
    	  int maxi = 0;
    	  int maxIndex = nums.length - 1;
    	  if (maxIndex == 0)
    		   return 0;
    	  int count = 0;
    	  for (int i = 0; i <= lastmax; i++) {
    		   if (i + nums[i] > maxJump) {
    				maxJump = i + nums[i];
    				maxi = i;
    		   }
    		   if (i == lastmax) {
    				if (lastmax != maxJump)
    					 count++;
    				i = maxi;
    				lastmax = maxJump;
    			 	maxJump = lastmax;
    		   }
    		   if (lastmax >= maxIndex)
    				return ++count;
    	  }
    	  return -1;
    }
    

    Why we need the code: maxJump = lastmax; it seems meaningless after lastmax = maxJump; but it will TLE without the code.


  • 0

    You don't need maxJump = lastmax;. I just submitted it without that, five times, and every time got it accepted (with 3 ms).

    Having maxJump = lastmax; indeed doesn't make sense. Why did you write it?


  • 0
    G

    thanks for your reply. This code is from Discuss. I don't think we need it. Now I retry and it was accept. Maybe the Leecode change something now.


  • 0

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.