-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0650-2-keys-keyboard.py
More file actions
28 lines (22 loc) · 810 Bytes
/
0650-2-keys-keyboard.py
File metadata and controls
28 lines (22 loc) · 810 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
28
# time complexity: O(n^2)
# space complexity: O(n^2)
class Solution:
def minSteps(self, n: int) -> int:
if n == 1:
return 0
self.n = n
self.memo = [[0] * (n // 2 + 1) for _ in range(n + 1)]
return 1 + self.minStepsHelper(1, 1)
def minStepsHelper(self, currLen: int, pasteLen: int) -> int:
if currLen == self.n:
return 0
if currLen > self.n:
return 1000
if self.memo[currLen][pasteLen] != 0:
return self.memo[currLen][pasteLen]
opt1 = 1 + self.minStepsHelper(currLen + pasteLen, pasteLen)
opt2 = 2 + self.minStepsHelper(currLen * 2, currLen)
self.memo[currLen][pasteLen] = min(opt1, opt2)
return self.memo[currLen][pasteLen]
n = 3
print(Solution().minSteps(n))