Eat Study Love

먹고 공부하고 사랑하라

Coding_Practice

Minimum One Bit Operations to Make Integers Zero[Dynamic Programming,Bit Manipulation,Memoization]

eatplaylove 2024. 11. 8. 13:09

https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/description/

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 representation of n.
  • Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.

Return the minimum number of operations to transform n into 0.

 

Example 1:

Input: n = 3
Output: 2
Explanation: The binary representation of 3 is "11".
"11" -> "01" with the 2nd operation since the 0th bit is 1.
"01" -> "00" with the 1st operation.

Example 2:

Input: n = 6
Output: 4
Explanation: The binary representation of 6 is "110".
"110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
"010" -> "011" with the 1st operation.
"011" -> "001" with the 2nd operation since the 0th bit is 1.
"001" -> "000" with the 1st operation.

 

Constraints:

  • 0 <= n <= 109

희한하게도, 평범한 문제처럼 보이는데 카테고리는 DP다.

 

1. C
비트로 어쩌고 저쩌고 하는 건 봐도 모르겄다;;;

int minimumOneBitOperations(int n) {
    if (n == 0) {
        return 0;
    }
    
    // 가장 높은 비트 찾기
    int k = 30;  // 31로 하면 오버플로우 발생 가능성
    while (k >= 0 && !(n & (1 << k))) {
        k--;
    }
    
    // Gray 코드 특성을 이용한 계산
    // (1 << (k + 1)) - 1을 오버플로우 없이 계산
    unsigned int result = (1u << (k + 1)) - 1;
    return result - minimumOneBitOperations(n ^ (1 << k));
}

 

2. Python

class Solution:
    def minimumOneBitOperations(self, n: int) -> int:
        if n == 0:
            return 0
        
        # 가장 높은 비트 찾기
        k = 0
        temp = n
        while temp:
            k += 1
            temp >>= 1
        k -= 1
        
        # Gray 코드 특성을 이용한 계산
        # f(2^k) = 2^(k+1) - 1
        # f(bxxxxxxx) = f(2^k) - f(remaining)
        return (1 << (k + 1)) - 1 - self.minimumOneBitOperations(n ^ (1 << k))
        
class Solution:
    def minimumOneBitOperations(self, n: int) -> int:
        if n == 0:
            return 0
        
        k = 0
        curr = 1
        while (curr * 2) <= n:
            curr *= 2
            k += 1

        return 2 ** (k + 1) - 1 - self.minimumOneBitOperations(n ^ curr)