Skip to content

Commit f7264a5

Browse files
authored
Create 664. Strange Printer1
1 parent 549d852 commit f7264a5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

664. Strange Printer1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
int dp[101][101];
3+
private:
4+
int f(int i, int j, string& s){
5+
if(i>j) return 0;
6+
if(dp[i][j]!=-1) return dp[i][j];
7+
8+
int steps = 1 + f(i+1,j,s);
9+
for(int k=i+1; k<=j; k++){
10+
if(s[k]==s[i]){
11+
steps = min(steps, f(i,k-1,s)+f(k+1,j,s));
12+
}
13+
}
14+
15+
return dp[i][j] = steps;
16+
}
17+
public:
18+
int strangePrinter(string s) {
19+
memset(dp,-1,sizeof(dp));
20+
return f(0,s.size()-1,s);
21+
}
22+
};

0 commit comments

Comments
 (0)