Skip to content

Commit f313005

Browse files
committed
Time: 3 ms (17.39%), Space: 17.8 MB (46.74%) - LeetHub
1 parent 6aa4e34 commit f313005

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# time complexity: O(log^2 n)
2+
# space complexity: O(logn)
3+
class Solution:
4+
def minimumOneBitOperations(self, n: int) -> int:
5+
if n == 0:
6+
return 0
7+
k = 0
8+
curr = 1
9+
while (curr * 2) <= n:
10+
curr *= 2
11+
k += 1
12+
return 2 ** (k + 1) - 1 - self.minimumOneBitOperations(n ^ curr)
13+
14+
15+
n = 3
16+
print(Solution().minimumOneBitOperations(n))
17+
n = 6
18+
print(Solution().minimumOneBitOperations(n))

0 commit comments

Comments
 (0)