|  | 
| 5 | 5 | // #Big_O_Time_O(n)_Space_O(1) #2024_11_10_Time_0_ms_(100.00%)_Space_44.9_MB_(75.73%) | 
| 6 | 6 | 
 | 
| 7 | 7 | class Solution { | 
| 8 |  | -    public int jump(int[] nums) { | 
| 9 |  | -        if (nums.length == 1) return 0; | 
| 10 |  | -        return minJumps(nums, 0, 0); | 
|  | 8 | +  public int jump(int[] nums) { | 
|  | 9 | +    if (nums.length == 1) { | 
|  | 10 | +      return 0; | 
| 11 | 11 |     } | 
|  | 12 | +    return minJumps(nums, 0, 0); | 
|  | 13 | +  } | 
| 12 | 14 | 
 | 
| 13 |  | -    private static int minJumps(int[] nums, int currIndex, int jumpStatus) { | 
| 14 |  | -        if (currIndex + nums[currIndex] >= nums.length - 1) return jumpStatus + 1; | 
| 15 |  | -        int nextIndex = currIndex + 1; | 
| 16 |  | -        int distanceLeft = (nums.length - 1 - (currIndex + 1)) - nums[currIndex + 1]; | 
| 17 |  | -        for (int i = currIndex + 2; i <= (currIndex + nums[currIndex]); i++) { | 
| 18 |  | -            int tempDistanceLeft = (nums.length - 1 - i) - nums[i]; | 
| 19 |  | -            if (distanceLeft > tempDistanceLeft) { | 
| 20 |  | -                distanceLeft = tempDistanceLeft; | 
| 21 |  | -                nextIndex = i; | 
| 22 |  | -            } | 
| 23 |  | -        } | 
| 24 |  | -        return minJumps(nums, nextIndex, jumpStatus + 1); | 
|  | 15 | + | 
|  | 16 | +  private static int minJumps(int[] nums, int currIndex, int jumpStatus) { | 
|  | 17 | +    if (currIndex + nums[currIndex] >= nums.length - 1) { | 
|  | 18 | +      return jumpStatus + 1; | 
|  | 19 | +    } | 
|  | 20 | +    int nextIndex = currIndex + 1; | 
|  | 21 | +    int distanceLeft = (nums.length - 1 - (currIndex + 1)) - nums[currIndex + 1]; | 
|  | 22 | +    for (int i = currIndex + 2; i <= (currIndex + nums[currIndex]); i++) { | 
|  | 23 | +      int tempDistanceLeft = (nums.length - 1 - i) - nums[i]; | 
|  | 24 | +      if (distanceLeft > tempDistanceLeft) { | 
|  | 25 | +        distanceLeft = tempDistanceLeft; | 
|  | 26 | +        nextIndex = i; | 
|  | 27 | +      } | 
| 25 | 28 |     } | 
|  | 29 | +    return minJumps(nums, nextIndex, jumpStatus + 1); | 
|  | 30 | +  } | 
| 26 | 31 | } | 
0 commit comments