Skip to content

Commit c53bfa6

Browse files
add 650
1 parent 4509d34 commit c53bfa6

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|650|[2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_650.java) | O(n^2) |O(n) | Medium | DP
2324
|648|[Replace Words](https://leetcode.com/problems/replace-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_648.java) | O(n) |O(n) | Medium | Trie
2425
|647|[Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_647.java) | O(n^2) |O(1) | Medium | DP
2526
|646|[Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_646.java) | O(nlogn) |O(1) | Medium | DP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 650. 2 Keys Keyboard
5+
*
6+
* Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
7+
8+
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
9+
Paste: You can paste the characters which are copied last time.
10+
Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.
11+
12+
Example 1:
13+
Input: 3
14+
Output: 3
15+
16+
Explanation:
17+
Intitally, we have one character 'A'.
18+
In step 1, we use Copy All operation.
19+
In step 2, we use Paste operation to get 'AA'.
20+
In step 3, we use Paste operation to get 'AAA'.
21+
22+
Note:
23+
The n will be in the range [1, 1000].
24+
*/
25+
public class _650 {
26+
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--) {//traverse backwards, whenever it's divisible by j, we'll update dp[i] because it's guaranteed to be smaller when j is smaller.
32+
if (i % j == 0) {
33+
dp[i] = dp[j] + (i/j);
34+
break;
35+
}
36+
}
37+
}
38+
return dp[n];
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._650;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 7/30/17.
11+
*/
12+
public class _650Test {
13+
private static _650 test;
14+
15+
@BeforeClass
16+
public static void setup(){
17+
test = new _650();
18+
}
19+
20+
@Test
21+
public void test1(){
22+
assertEquals(3, test.minSteps(3));
23+
}
24+
25+
@Test
26+
public void test2(){
27+
assertEquals(9, test.minSteps(20));
28+
}
29+
30+
@Test
31+
public void test3(){
32+
assertEquals(19, test.minSteps(19));
33+
}
34+
35+
@Test
36+
public void test4(){
37+
assertEquals(0, test.minSteps(1));
38+
}
39+
40+
@Test
41+
public void test5(){
42+
assertEquals(35, test.minSteps(741));
43+
}
44+
}

0 commit comments

Comments
 (0)