Minimum Flips to Make a OR b Equal to c LeetCode solution in Python
1# Runtime: 37 ms (Top 81.40%) | Memory: 13.8 MB (Top 80.93%)2class Solution:3def minFlips(self, a: int, b: int, c: int) -> int:4res = 05for i in range(32):6if (a & 1) | (b & 1) != (c & 1):7if (8c & 19) == 1: # (a & 1) | (b & 1) should be == 1 ; so changing any of a, b we can get 110res += 111else: # (a & 1) | (b & 1) should be == 0 ; is (a & 1) == 1 and (b & 1) == 1 we need to change both to 0 so res += 1; if any of them is 1 then change only 1 i.e. res += 112res += (a & 1) + (b & 1)13a, b, c = a >> 1, b >> 1, c >> 1 # right-shift by 11415return res161718# Time: O(1)19# Space: O(1)