Skip to content

Commit d72d1a8

Browse files
committed
Runtime: 42 ms (Top 77.23%) | Memory: 14 MB (Top 29.19%)
1 parent 46dc293 commit d72d1a8

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
# Runtime: 42 ms (Top 77.23%) | Memory: 14 MB (Top 29.19%)
2+
13
class Solution:
24
def integerReplacement(self, n: int) -> int:
35
dp = {}
46
def dfs(num):
57
if num == 1:
68
return 0
7-
9+
810
if num in dp:
911
return dp[num]
10-
12+
1113
# if num is even, we have only one option -> n / 2
1214
even = odd = 0
1315
if num % 2 == 0:
@@ -18,8 +20,8 @@ def dfs(num):
1820
odd2 = 1 + dfs(num + 1)
1921
# take the min of both operation
2022
odd = min(odd1, odd2)
21-
23+
2224
dp[num] = even + odd
2325
return dp[num]
2426

25-
return dfs(n)
27+
return dfs(n)

0 commit comments

Comments
 (0)