-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0664-strange-printer.js
35 lines (32 loc) · 1008 Bytes
/
0664-strange-printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 664. Strange Printer
* https://leetcode.com/problems/strange-printer/
* Difficulty: Hard
*
* There is a strange printer with the following two special properties:
* - The printer can only print a sequence of the same character each time.
* - At each turn, the printer can print new characters starting from and ending at any place and
* will cover the original existing characters.
*
* Given a string s, return the minimum number of turns the printer needed to print it.
*/
/**
* @param {string} s
* @return {number}
*/
var strangePrinter = function(s) {
const n = s.length;
const dp = new Array(n).fill().map(() => new Array(n).fill(0));
for (let i = n - 1; i >= 0; i--) {
dp[i][i] = 1;
for (let j = i + 1; j < n; j++) {
dp[i][j] = dp[i][j - 1] + 1;
for (let k = i; k < j; k++) {
if (s[k] === s[j]) {
dp[i][j] = Math.min(dp[i][j], dp[i][k] + (k + 1 <= j - 1 ? dp[k + 1][j - 1] : 0));
}
}
}
}
return dp[0][n - 1];
};