Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 955b4d9

Browse files
refactor 650
1 parent 23e60ed commit 955b4d9

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@
2424
*/
2525
public class _650 {
2626

27-
public int minSteps(int n) {
28-
int[] dp = new int[n + 1];
29-
for (int i = 2; i <= n; i++) {
30-
dp[i] = i;//we assign i to dp[i] first, because for a lot of cases, e.g. for most cases when i is odd, its min steps is i itself, if it's not, we can overwrite it later
31-
for (int j = i - 1; j > 1; j--) {
32-
//traverse backwards, whenever it's divisible by j, we'll update dp[i] because it's guaranteed to be smaller when j is smaller.
33-
if (i % j == 0) {
34-
dp[i] = dp[j] + (i / j);
35-
break;
27+
public static class Solution1 {
28+
public int minSteps(int n) {
29+
int[] dp = new int[n + 1];
30+
for (int i = 2; i <= n; i++) {
31+
dp[i] = i;//we assign i to dp[i] first, because for a lot of cases, e.g. for most cases when i is odd, its min steps is i itself, if it's not, we can overwrite it later
32+
for (int j = i - 1; j > 1; j--) {
33+
//traverse backwards, whenever it's divisible by j, we'll update dp[i] because it's guaranteed to be smaller when j is smaller.
34+
if (i % j == 0) {
35+
dp[i] = dp[j] + (i / j);
36+
break;
37+
}
3638
}
3739
}
40+
return dp[n];
3841
}
39-
return dp[n];
4042
}
4143
}

src/test/java/com/fishercoder/_650Test.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _650Test {
10-
private static _650 test;
10+
private static _650.Solution1 solution1;
1111

1212
@BeforeClass
1313
public static void setup() {
14-
test = new _650();
14+
solution1 = new _650.Solution1();
1515
}
1616

1717
@Test
1818
public void test1() {
19-
assertEquals(3, test.minSteps(3));
19+
assertEquals(3, solution1.minSteps(3));
2020
}
2121

2222
@Test
2323
public void test2() {
24-
assertEquals(9, test.minSteps(20));
24+
assertEquals(9, solution1.minSteps(20));
2525
}
2626

2727
@Test
2828
public void test3() {
29-
assertEquals(19, test.minSteps(19));
29+
assertEquals(19, solution1.minSteps(19));
3030
}
3131

3232
@Test
3333
public void test4() {
34-
assertEquals(0, test.minSteps(1));
34+
assertEquals(0, solution1.minSteps(1));
3535
}
3636

3737
@Test
3838
public void test5() {
39-
assertEquals(35, test.minSteps(741));
39+
assertEquals(35, solution1.minSteps(741));
4040
}
4141
}

0 commit comments

Comments
 (0)