Skip to content

Commit 50f9186

Browse files
refactor 651
1 parent 955b4d9 commit 50f9186

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,26 @@ Now, you can only press the keyboard for N times (with the above four keys), fin
3535
*/
3636
public class _651 {
3737

38-
/**Minimum needs to be more than 3 A's in a row, otherwise "Ctrl A, Ctrl C, Ctrl V" will make fewer A's than directly
39-
* copying A's with the equal number of steps.
40-
* E.g. when n == 5,
41-
* if we do 5 this: A, A, Ctrl A, Ctrl C, Ctrl V, => this will result in only AAAA (4 A's)
42-
* while if we do A, A, A, A, A, => this will result in AAAAA (5 A's)
43-
* So, at a minimum, we need to have 3 A's, then it's worth to do "Ctrl A, Ctrl C, Ctrl V"..
44-
* That's why we have j = 3 in the inner for loop below.
45-
* */
46-
public int maxA(int N) {
47-
int[] dp = new int[N + 1];
48-
for (int i = 1; i <= N; i++) {
49-
dp[i] = i;
50-
for (int j = 3; j < i; j++) {
51-
dp[i] = Math.max(dp[i], dp[i - j] * (j - 1));
38+
public static class Solution1 {
39+
/**
40+
* Minimum needs to be more than 3 A's in a row, otherwise "Ctrl A, Ctrl C, Ctrl V" will make fewer A's than directly
41+
* copying A's with the equal number of steps.
42+
* E.g. when n == 5,
43+
* if we do 5 this: A, A, Ctrl A, Ctrl C, Ctrl V, => this will result in only AAAA (4 A's)
44+
* while if we do A, A, A, A, A, => this will result in AAAAA (5 A's)
45+
* So, at a minimum, we need to have 3 A's, then it's worth to do "Ctrl A, Ctrl C, Ctrl V"..
46+
* That's why we have j = 3 in the inner for loop below.
47+
*/
48+
public int maxA(int N) {
49+
int[] dp = new int[N + 1];
50+
for (int i = 1; i <= N; i++) {
51+
dp[i] = i;
52+
for (int j = 3; j < i; j++) {
53+
dp[i] = Math.max(dp[i], dp[i - j] * (j - 1));
54+
}
5255
}
56+
return dp[N];
5357
}
54-
return dp[N];
5558
}
5659

5760
}

src/test/java/com/fishercoder/_651Test.java

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

99
public class _651Test {
10-
private static _651 test;
10+
private static _651.Solution1 solution1;
1111

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

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

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

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

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

3737
@Test
3838
public void test5() {
39-
assertEquals(1327104, test.maxA(50));
39+
assertEquals(1327104, solution1.maxA(50));
4040
}
4141

4242
@Test
4343
public void test6() {
44-
assertEquals(9, test.maxA(7));
44+
assertEquals(9, solution1.maxA(7));
4545
}
4646
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy