Minimum One Bit Operations to Make Integers Zero LeetCode solution in Python

1
class Solution:
2
def minimumOneBitOperations(self, n: int) -> int:
3
if n <= 1:
4
return n
5

6
def leftmostbit(x):
7
x |= x >> 1
8
x |= x >> 2
9
x |= x >> 4
10
x |= x >> 8
11
x |= x >> 16
12
x += 1
13
x >>= 1
14
return x
15

16
x = leftmostbit(n)
17
return ((x << 1) - 1) - self.minimumOneBitOperations(n - x)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0