-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path4-keys-keyboard.py
More file actions
27 lines (22 loc) · 912 Bytes
/
4-keys-keyboard.py
File metadata and controls
27 lines (22 loc) · 912 Bytes
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
class Solution:
def maxA(self, N: int) -> int:
dp = [0] * (N + 3)
for pos in range(3, N + 3):
dp[pos] = max(
dp[pos - 1] + 1,
dp[pos - 3] * 2,
dp[pos - 4] * 3,
dp[pos - 5] * 4,
)
return dp[-1]
def maxABruteForce(self, N: int) -> int:
def dp(pos: int, printed_size: int, selected: bool, buffer_size: int) -> int:
if pos == N:
return printed_size
return max(
dp(pos + 1, printed_size + 1, False, 0), # add 1 character
dp(pos + 1, printed_size, True, 0), # select
dp(pos + 1, printed_size, False, printed_size) if selected else 0, # copy into buffer
dp(pos + 1, printed_size + buffer_size, False, buffer_size), # paste buffer
)
return dp(0, 0, False, 0)