We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 46dc293 commit d72d1a8Copy full SHA for d72d1a8
scripts/algorithms/I/Integer Replacement/Integer Replacement.py
@@ -1,13 +1,15 @@
1
+# Runtime: 42 ms (Top 77.23%) | Memory: 14 MB (Top 29.19%)
2
+
3
class Solution:
4
def integerReplacement(self, n: int) -> int:
5
dp = {}
6
def dfs(num):
7
if num == 1:
8
return 0
-
9
10
if num in dp:
11
return dp[num]
12
13
# if num is even, we have only one option -> n / 2
14
even = odd = 0
15
if num % 2 == 0:
@@ -18,8 +20,8 @@ def dfs(num):
18
20
odd2 = 1 + dfs(num + 1)
19
21
# take the min of both operation
22
odd = min(odd1, odd2)
23
24
dp[num] = even + odd
25
26
- return dfs(n)
27
+ return dfs(n)
0 commit comments