Skip to content

Commit 4b92032

Browse files
refactor 45
1 parent 3555172 commit 4b92032

File tree

1 file changed

+13
-30
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-30
lines changed
Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 45. Jump Game II
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-
* Your goal is to reach the last index in the minimum number of jumps.
9-
10-
For example:
11-
Given array A = [2,3,1,1,4]
12-
13-
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
14-
15-
Note:
16-
You can assume that you can always reach the last index.
17-
18-
*/
193
public class _45 {
20-
21-
public static class Solution1 {
22-
public int jump(int[] nums) {
23-
int stepCount = 0;
24-
int lastJumpMax = 0;
25-
int currentJumpMax = 0;
26-
for (int i = 0; i < nums.length - 1; i++) {
27-
currentJumpMax = Math.max(currentJumpMax, i + nums[i]);
28-
if (i == lastJumpMax) {
29-
stepCount++;
30-
lastJumpMax = currentJumpMax;
4+
public static class Solution1 {
5+
public int jump(int[] nums) {
6+
int stepCount = 0;
7+
int lastJumpMax = 0;
8+
int currentJumpMax = 0;
9+
for (int i = 0; i < nums.length - 1; i++) {
10+
currentJumpMax = Math.max(currentJumpMax, i + nums[i]);
11+
if (i == lastJumpMax) {
12+
stepCount++;
13+
lastJumpMax = currentJumpMax;
14+
}
15+
}
16+
return stepCount;
3117
}
32-
}
33-
return stepCount;
3418
}
35-
}
3619
}

0 commit comments

Comments
 (0)