-
Notifications
You must be signed in to change notification settings - Fork 17
/
integer-break.py
36 lines (28 loc) · 984 Bytes
/
integer-break.py
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
from functools import lru_cache
class Solution:
def integerBreak(self, n: int) -> int:
dp = [0] * (n + 1)
for num in range(n + 1):
for split in range(1, num // 2 + 1):
dp[num] = max(
dp[num],
split * (num - split),
dp[split] * dp[num - split],
split * dp[num - split],
dp[split] * (num - split),
)
return dp[n]
def integerBreakTopDown(self, n: int) -> int:
@lru_cache(None)
def dfs(num: int) -> int:
result = 0
for split in range(1, num // 2 + 1):
result = max(
result,
split * (num - split),
dfs(split) * dfs(num - split),
split * dfs(num - split),
dfs(split) * (num - split),
)
return result
return dfs(n)