forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
57 lines (40 loc) · 1.27 KB
/
main.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// Source : https://leetcode.com/problems/strange-printer/
/// Author : liuyubobobo
/// Time : 2020-10-16
#include <iostream>
#include <vector>
using namespace std;
/// Interval DP
/// Time Complexity: O(n^3)
/// Space Complexity: O(n^2)
class Solution {
public:
int strangePrinter(string s) {
vector<vector<int>> dp(s.size(), vector<int>(s.size(), -1));
return dfs(s, 0, s.size() - 1, dp);
}
private:
int dfs(const string& s, int l, int r, vector<vector<int>>& dp){
if(l > r) return 0;
if(l == r) return 1;
if(dp[l][r] != -1) return dp[l][r];
int res = 1 + dfs(s, l + 1, r, dp);
for(int i = l + 1; i <= r; i ++)
if(s[i] == s[l])
res = min(res, dfs(s, l, i - 1, dp) + dfs(s, i + 1, r, dp));
return dp[l][r] = res;
}
};
int main() {
cout << Solution().strangePrinter("aaabbb") << endl;
// 2
cout << Solution().strangePrinter("aba") << endl;
// 2
cout << Solution().strangePrinter("aaaaaaaaaaaaaaaaaaaaa") << endl;
// 1
cout << Solution().strangePrinter("baacdddaaddaaaaccbddbcabdaabdbbcdcbbbacbddcabcaaa") << endl;
// 19
cout << Solution().strangePrinter("dddccbdbababaddcbcaabdbdddcccddbbaabddb") << endl;
// 15
return 0;
}