Skip to content

Commit efad02f

Browse files
add 664
1 parent 6f14379 commit efad02f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

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

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|664|[Strange Printer](https://leetcode.com/problems/strange-printer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_664.java) | O(n^3) | O(n^2) | Hard | DP
2526
|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | O(n) | O(n) | Medium | Tree
2627
|662|[Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_662.java) | O(n) | O(k) | Medium | BFS, DFS
2728
|661|[Image Smoother](https://leetcode.com/problems/image-smoother/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_661.java) | O(m*n) | O(1) | Easy | Array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 664. Strange Printer
5+
*
6+
* There is a strange printer with the following two special requirements:
7+
* The printer can only print a sequence of the same character each time.
8+
* At each turn, the printer can print new characters starting from and ending at any places, and will cover the original existing characters.
9+
* Given a string consists of lower English letters only, your job is to count the minimum number of turns the printer needed in order to print it.
10+
11+
Example 1:
12+
Input: "aaabbb"
13+
Output: 2
14+
Explanation: Print "aaa" first and then print "bbb".
15+
16+
Example 2:
17+
Input: "aba"
18+
Output: 2
19+
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
20+
21+
Hint: Length of the given string will not exceed 100.
22+
*/
23+
public class _664 {
24+
/**reference: https://discuss.leetcode.com/topic/100137/java-solution-dp*/
25+
public int strangePrinter(String s) {
26+
int n = s.length();
27+
if (n == 0) return 0;
28+
29+
int[][] dp = new int[101][101];
30+
for (int i = 0; i < n; i++) {
31+
dp[i][i] = 1;
32+
}
33+
34+
for (int i = 1; i < n; i++) {
35+
for (int j = 0; j < n - i; j++) {
36+
dp[j][j + i] = i + 1;
37+
for (int k = j + 1; k <= j + i; k++) {
38+
int temp = dp[j][k - 1] + dp[k][j + i];
39+
if (s.charAt(k - 1) == s.charAt(j + i)) {
40+
temp--;
41+
}
42+
dp[j][j + i] = Math.min(dp[j][j + i], temp);
43+
}
44+
}
45+
}
46+
return dp[0][n - 1];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._664;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _664Test {
10+
private static _664 test;
11+
12+
@BeforeClass
13+
public static void setup(){
14+
test = new _664();
15+
}
16+
17+
@Test
18+
public void test1(){
19+
assertEquals(2, test.strangePrinter("aaabbb"));
20+
}
21+
22+
@Test
23+
public void test2(){
24+
assertEquals(2, test.strangePrinter("aba"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)