|
5 | 5 | */ |
6 | 6 | public class JumpGameII { |
7 | 7 |
|
| 8 | + // runtime: O(N^2) |
| 9 | + // space: O(N) |
| 10 | + public static int jumpON2(int[] nums) { |
| 11 | + int n = nums.length; |
| 12 | + int[] counts = new int[n]; |
| 13 | + for (int i = n - 2; i >= 0; i--) { |
| 14 | + int jumps = Integer.MAX_VALUE; |
| 15 | + for (int j = 1; j <= nums[i] && i + j < n; j++) { |
| 16 | + int c = counts[i + j]; |
| 17 | + if (c != Integer.MAX_VALUE) { |
| 18 | + jumps = Math.min(jumps, 1 + c); |
| 19 | + } |
| 20 | + |
| 21 | + } |
| 22 | + counts[i] = jumps; |
| 23 | + } |
| 24 | + |
| 25 | + return counts[0]; |
| 26 | + } |
| 27 | + |
| 28 | + // runtime: O(N) |
| 29 | + // space: O(1) |
| 30 | + // https://leetcode.com/problems/jump-game-ii/editorial/ |
| 31 | + public static int jumpGreedy(int[] nums) { |
| 32 | + // The starting range of the first jump is [0, 0] |
| 33 | + int answer = 0, n = nums.length; |
| 34 | + int curEnd = 0, curFar = 0; |
| 35 | + |
| 36 | + for (int i = 0; i < n - 1; ++i) { |
| 37 | + // Update the farthest reachable index of this jump. |
| 38 | + curFar = Math.max(curFar, i + nums[i]); |
| 39 | + |
| 40 | + // If we finish the starting range of this jump, |
| 41 | + // Move on to the starting range of the next jump. |
| 42 | + if (i == curEnd) { |
| 43 | + answer++; |
| 44 | + curEnd = curFar; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return answer; |
| 49 | + } |
| 50 | + |
8 | 51 | public static int jumpON(int[] nums) { |
9 | 52 | if (nums == null) { |
10 | 53 | return 0; |
@@ -32,31 +75,33 @@ public static int jumpON(int[] nums) { |
32 | 75 | return jumpCount; |
33 | 76 | } |
34 | 77 |
|
| 78 | + // https://leetcode.com/problems/jump-game-ii/editorial/comments/1776566 |
35 | 79 | public static int jump(int[] nums) { |
36 | 80 | if (nums == null) { |
37 | 81 | return 0; |
38 | 82 | } |
39 | 83 |
|
40 | | - int steps = 0; |
41 | | - for (int l = 0, r = 0; r < nums.length - 1; steps++) { |
42 | | - int next = r; |
43 | | - for (int i = l; i <= r; i++) { |
44 | | - next = Math.max(next, nums[i] + i); |
| 84 | + int steps = 0, low = 0, high = 0, n = nums.length; |
| 85 | + while (high < n - 1) { |
| 86 | + int maxJump = 0; |
| 87 | + for (int i = low; i <= high; i++) { |
| 88 | + maxJump = Math.max(maxJump, i + nums[i]); |
45 | 89 | } |
46 | | - l = r + 1; |
47 | | - r = next; |
| 90 | + low = high + 1; |
| 91 | + high = maxJump; |
| 92 | + steps++; |
48 | 93 | } |
49 | 94 |
|
50 | 95 | return steps; |
51 | 96 | } |
52 | 97 |
|
53 | 98 | public static void main(String[] args) { |
54 | | -// int[] jumps = {2, 3, 1, 1, 4}; |
55 | | - int[] jumps = {4, 1, 1, 3, 1, 1, 1}; |
56 | | -// int[] jumps = {1, 2, 1, 1, 1}; |
57 | | -// int[] jumps = {10,9,8,7,6,5,4,3,2,1,1,0}; |
58 | | - System.out.println(jump(jumps)); |
59 | | - System.out.println(jumpON(jumps)); |
| 99 | + // int[] jumps = {2, 3, 1, 1, 4}; |
| 100 | + int[] jumps = { 4, 1, 1, 3, 1, 1, 1 }; |
| 101 | + // int[] jumps = {1, 2, 1, 1, 1}; |
| 102 | + // int[] jumps = {10,9,8,7,6,5,4,3,2,1,1,0}; |
| 103 | + System.out.println(jumpGreedy(jumps)); |
| 104 | + // System.out.println(jumpON(jumps)); |
60 | 105 | } |
61 | 106 |
|
62 | 107 | } |
0 commit comments