Skip to content

Commit 36183c8

Browse files
refactor 656
1 parent ebaf443 commit 36183c8

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.ArrayList;
54
import java.util.Arrays;
65
import java.util.List;
@@ -35,38 +34,40 @@
3534
*/
3635

3736
public class _656 {
37+
public static class Solution1 {
3838

39-
/**
40-
* Time: O(n*B)
41-
* Reference: https://leetcode.com/articles/coin-path/#approach-3-using-dynamic-programming-accepted
42-
*/
43-
public List<Integer> cheapestJump(int[] A, int B) {
44-
int[] next = new int[A.length];
45-
long[] dp = new long[A.length];
46-
Arrays.fill(next, -1);
47-
List<Integer> res = new ArrayList();
48-
for (int i = A.length - 2; i >= 0; i--) {
49-
long minCost = Integer.MAX_VALUE;
50-
for (int j = i + 1; j <= i + B && j < A.length; j++) {
51-
if (A[j] >= 0) {
52-
long cost = A[i] + dp[j];
53-
if (cost < minCost) {
54-
minCost = cost;
55-
next[i] = j;
39+
/**
40+
* Time: O(n*B)
41+
* Reference: https://leetcode.com/articles/coin-path/#approach-3-using-dynamic-programming-accepted
42+
*/
43+
public List<Integer> cheapestJump(int[] A, int B) {
44+
int[] next = new int[A.length];
45+
long[] dp = new long[A.length];
46+
Arrays.fill(next, -1);
47+
List<Integer> res = new ArrayList();
48+
for (int i = A.length - 2; i >= 0; i--) {
49+
long minCost = Integer.MAX_VALUE;
50+
for (int j = i + 1; j <= i + B && j < A.length; j++) {
51+
if (A[j] >= 0) {
52+
long cost = A[i] + dp[j];
53+
if (cost < minCost) {
54+
minCost = cost;
55+
next[i] = j;
56+
}
5657
}
5758
}
59+
dp[i] = minCost;
5860
}
59-
dp[i] = minCost;
60-
}
61-
int i;
62-
for (i = 0; i < A.length && next[i] > 0; i = next[i]) {
63-
res.add(i + 1);
64-
}
65-
if (i == A.length - 1 && A[i] >= 0) {
66-
res.add(A.length);
67-
} else {
68-
return new ArrayList<>();
61+
int i;
62+
for (i = 0; i < A.length && next[i] > 0; i = next[i]) {
63+
res.add(i + 1);
64+
}
65+
if (i == A.length - 1 && A[i] >= 0) {
66+
res.add(A.length);
67+
} else {
68+
return new ArrayList<>();
69+
}
70+
return res;
6971
}
70-
return res;
7172
}
7273
}

src/test/java/com/fishercoder/_656Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
* Created by fishercoder on 5/25/17.
1515
*/
1616
public class _656Test {
17-
private static _656 test;
17+
private static _656.Solution1 solution1;
1818
private static int[] A;
1919
private static List<Integer> expected;
2020

2121
@BeforeClass
2222
public static void setup() {
23-
test = new _656();
23+
solution1 = new _656.Solution1();
2424
}
2525

2626
@Test
2727
public void test1() {
2828
A = new int[]{1, 2, 4, -1, 2};
2929
expected = new ArrayList<>(Arrays.asList(1, 3, 5));
30-
assertEquals(expected, test.cheapestJump(A, 2));
30+
assertEquals(expected, solution1.cheapestJump(A, 2));
3131
}
3232
}

0 commit comments

Comments
 (0)