File tree 3 files changed +76
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
22
22
23
23
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
24
24
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
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
25
26
|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
26
27
|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
27
28
|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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments