Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50e6123

Browse files
Daily Solution In JS
1 parent 5e94c31 commit 50e6123

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎Hard/664. Stranger Printer.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var strangePrinter = function(s) {
2+
const n = s.length;
3+
const memo = Array(n).fill().map(() => Array(n).fill(0));
4+
5+
function dp(l, r) {
6+
if (l>r) return 0;
7+
8+
if (memo[l][r]) return memo[l][r];
9+
10+
let min = 1 + dp(l+1, r);
11+
12+
for (let i=l+1; i<=r; ++i)
13+
if (s[i]==s[l])
14+
min = Math.min(min, dp(l+1, i-1) + dp(i, r));
15+
16+
memo[l][r] = min;
17+
return min;
18+
}
19+
20+
return dp(0, n-1);
21+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /