파비의 매일매일 공부기록

2023.06.07 Today's Challenge 본문

Problem Solving/LeetCode

2023.06.07 Today's Challenge

fabichoi 2023. 6. 7. 23:45

https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/

 

Minimum Flips to Make a OR b Equal to c - LeetCode

Can you solve this real interview question? Minimum Flips to Make a OR b Equal to c - Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consist

leetcode.com

비트 연산을 활용한 문제.
대부분의 문제가 그렇듯, 역으로 따져 올라가면 된다.

class Solution:
    def minFlips(self, a: int, b: int, c: int) -> int:
        answer = 0
        while a or b or c:
            if c & 1: 
                answer += 0 if ((a & 1) or (b & 1)) else 1
            else:
                answer += (a & 1) + (b & 1)
            a >>= 1
            b >>= 1
            c >>= 1
        
        return answer
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.06.09 Today's Challenge  (0) 2023.06.09
2023.06.08 Today's Challenge  (0) 2023.06.08
2023.06.06 Today's Challenge  (0) 2023.06.06
2023.06.05 Today's Challenge  (0) 2023.06.05
2023.06.04 Today's Challenge  (0) 2023.06.04
Comments