-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL664.py
More file actions
88 lines (73 loc) · 2.58 KB
/
L664.py
File metadata and controls
88 lines (73 loc) · 2.58 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 664. 奇怪的打印机
import math
class Solution:
def strangePrinter1(self, s: str) -> int:
n = len(s)
memo = [[-1] * n for _ in range(n)]
return self.dfs(s, 0, len(s) - 1, memo)
# 方式二:记忆化搜索
def dfs(self, s: str, start: int, end: int, memo: list[[]]) -> int:
if memo[start][end] != -1:
return memo[start][end]
ans = 0
if start == end:
ans = 1
elif start + 1 == end:
ans = 1 if s[start] == s[end] else 2
elif s[start] == s[end]:
ans = self.dfs(s, start, end - 1, memo)
else:
ans = float('inf')
for i in range(start, end): # range是左闭右开的end不能-1
t = self.dfs(s, start, i, memo) + self.dfs(s, i + 1, end, memo)
ans = min(ans, t)
memo[start][end] = ans
return ans
# 为什么不行
# def dfs(self, s: str, start: int, end: int) -> int:
# if start > end:
# return 0
# elif start == end:
# return 1
# elif start + 1 == end:
# return 1 if s[start] == s[end] else 2
# elif s[start] == s[end]:
# return self.dfs(s, start, end - 1)
# else:
# l: int = start + 1
# r: int = end - 1
# while s[l - 1] == s[l]:
# l += 1
# while s[r + 1] == s[r]:
# r -= 1
#
# return min(self.dfs(s, start, r), self.dfs(s, l, end)) + 1
# 方式三:二位动态规划
def strangePrinter(self, s: str) -> int:
n = len(s)
dp = [[0] * n for _ in range(n)]
for k in range(n):
start = 0
end = k
while start < n and end < n:
if start == end:
ans = 1
elif start + 1 == end:
ans = 1 if s[start] == s[end] else 2
elif s[start] == s[end]:
ans = dp[start][end - 1]
else:
ans = float('inf')
for i in range(start, end): # range是左闭右开的end不能-1
t = dp[start][i] + dp[i + 1][end]
ans = min(ans, t)
dp[start][end] = ans
start += 1
end += 1
return dp[0][n - 1]
if __name__ == '__main__':
s = Solution()
print(s.strangePrinter("AAB"))
print(s.strangePrinter("RGBGR"))
print(s.strangePrinter("aaaabbbb"))
print(s.strangePrinter("baacdddaaddaaaaccbddbcabdaabdbbcdcbbbacbddcabcaaa"))