Skip to content

Commit 6567f67

Browse files
refactor 45
1 parent 515724f commit 6567f67

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/main/java/com/fishercoder/solutions/_45.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
public class _45 {
44
public static class Solution1 {
55
public int jump(int[] nums) {
6-
int stepCount = 0;
6+
int jumps = 0;
77
int lastJumpMax = 0;
88
int currentJumpMax = 0;
99
for (int i = 0; i < nums.length - 1; i++) {
1010
currentJumpMax = Math.max(currentJumpMax, i + nums[i]);
1111
if (i == lastJumpMax) {
12-
stepCount++;
12+
jumps++;
1313
lastJumpMax = currentJumpMax;
1414
}
15+
if (lastJumpMax >= nums.length) {
16+
return jumps;
17+
}
1518
}
16-
return stepCount;
19+
return jumps;
1720
}
1821
}
1922
}

src/test/java/com/fishercoder/_45Test.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _45Test {
10-
private static _45.Solution1 solution1;
11-
private static int[] nums;
10+
private static _45.Solution1 solution1;
11+
private static int[] nums;
1212

13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _45.Solution1();
16-
}
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _45.Solution1();
16+
}
1717

18-
@Test
19-
public void test1() {
20-
nums = new int[] {2, 3, 1, 1, 4};
21-
assertEquals(2, solution1.jump(nums));
22-
}
18+
@Test
19+
public void test1() {
20+
nums = new int[]{2, 3, 1, 1, 4};
21+
assertEquals(2, solution1.jump(nums));
22+
}
2323
}

0 commit comments

Comments
 (0)