Skip to content

Commit 41119d8

Browse files
[N-0] refactor 55
1 parent 1d0f00c commit 41119d8

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,28 @@
11
package com.fishercoder.solutions;
22

3-
/**Given an array of non-negative integers, you are initially positioned at the first index of the array.
4-
5-
Each element in the array represents your maximum jump length at that position.
6-
7-
Determine if you are able to reach the last index.
8-
9-
For example:
10-
A = [2,3,1,1,4], return true.
11-
12-
A = [3,2,1,0,4], return false.*/
3+
/**
4+
* 55. Jump Game
5+
*
6+
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
7+
* Each element in the array represents your maximum jump length at that position.
8+
* Determine if you are able to reach the last index.
9+
*
10+
* For example:
11+
* A = [2,3,1,1,4], return true.
12+
* A = [3,2,1,0,4], return false.*/
1313
public class _55 {
1414

15-
public static boolean canJump_greedy(int[] nums) {
16-
int farthest = nums[0];
17-
for (int i = 0; i < nums.length; i++) {
18-
if (i <= farthest && nums[i] + i > farthest) {
19-
//i <= farthest is to make sure that this current i is within the current range
20-
// nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i
21-
farthest = nums[i] + i;
22-
}
23-
}
24-
return farthest >= nums.length - 1;
25-
}
26-
27-
//this normal dp ends in TLE for extreme test cases
28-
public static boolean canJump_dp(int[] nums) {
29-
boolean[] can = new boolean[nums.length];
30-
can[0] = true;
31-
for (int i = 0; i < nums.length; i++) {
32-
int reach = nums[i];
33-
if (can[i]) {
34-
for (int j = i + 1; j < nums.length && j <= i + reach; j++) {
35-
can[j] = true;
15+
public static class Solution1 {
16+
public boolean canJump(int[] nums) {
17+
int farthest = nums[0];
18+
for (int i = 0; i < nums.length; i++) {
19+
if (i <= farthest && nums[i] + i > farthest) {
20+
//i <= farthest is to make sure that this current i is within the current range
21+
// nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i
22+
farthest = nums[i] + i;
3623
}
3724
}
25+
return farthest >= nums.length - 1;
3826
}
39-
return can[nums.length - 1];
40-
}
41-
42-
public static void main(String... strings) {
43-
// int[] nums = new int[]{1,2};
44-
int[] nums = new int[]{0, 2, 3};
45-
System.out.println(canJump_greedy(nums));
4627
}
4728
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._55;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _55Test {
10+
private static _55.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _55.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{0, 2, 3};
21+
assertEquals(false, solution1.canJump(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{1, 2};
27+
assertEquals(true, solution1.canJump(nums));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)