-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2522.py
More file actions
71 lines (56 loc) · 1.74 KB
/
L2522.py
File metadata and controls
71 lines (56 loc) · 1.74 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
# 2522. 将字符串分割成值不超过 K 的子字符串
from functools import cache
class Solution:
def minimumPartition1(self, s: str, k: int) -> int:
n = len(s)
@cache
def dfs(index):
if index == n:
return 0
ans = float('inf')
i = index
t = ord(s[i]) - ord('0')
while t <= k:
i += 1
ans = min(ans, dfs(i))
if i >= n:
break
t *= 10
t += ord(s[i]) - ord('0')
return ans + 1
return -1 if dfs(0) == float('inf') else dfs(0)
def minimumPartition2(self, s: str, k: int) -> int:
n = len(s)
dp = [0] * (n + 1)
for index in range(n - 1, -1, -1):
ans = float('inf')
i = index
t = ord(s[i]) - ord('0')
while t <= k:
i += 1
ans = min(ans, dp[i])
if i >= n:
break
t *= 10
t += ord(s[i]) - ord('0')
dp[index] = ans + 1
return dp[0] if dp[0] != float('inf') else -1
# 贪心
def minimumPartition(self, s: str, k: int) -> int:
n = len(s)
x = 0
ans = 1 # ans是分割的点数,实际的子数组个数要加一,所以初始化为1
for i in range(0, n):
v = int(s[i])
if v > k: return -1
x *= 10
x += v
if x > k:
ans += 1
x = v
return ans
if __name__ == '__main__':
s = Solution()
print(s.minimumPartition("165462", 60))
print(s.minimumPartition("238182", 5))
print(s.minimumPartition("1", 1))