Problem Solving/LeetCode
2023.12.02 Today's Challenge
fabichoi
2023. 12. 2. 23:45
Minimum One Bit Operations to Make Integers Zero - LeetCode
Can you solve this real interview question? Minimum One Bit Operations to Make Integers Zero - Given an integer n, you must transform it into 0 using the following operations any number of times: * Change the rightmost (0th) bit in the binary representatio
leetcode.com
DP로 푸는 문제
class Solution:
def minimumOneBitOperations(self, n: int) -> int:
if not n:
return 0
k, curr = 0, 1
while (curr * 2) <= n:
curr *= 2
k += 1
return 2 ** (k+1) - 1 - self.minimumOneBitOperations(n ^ curr)
반응형