From 99d49194a44f42b82d713116e34d2880a91dbf3d Mon Sep 17 00:00:00 2001 From: ssun <56533266+blaire-pi@users.noreply.github.com> Date: Thu, 10 Apr 2025 14:11:30 +0900 Subject: [PATCH 1/2] Create 1318 yerin.py --- LeetCode/1318/yerin.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 LeetCode/1318/yerin.py diff --git a/LeetCode/1318/yerin.py b/LeetCode/1318/yerin.py new file mode 100644 index 0000000..1eb5e62 --- /dev/null +++ b/LeetCode/1318/yerin.py @@ -0,0 +1,26 @@ +class Solution: + def dec_to_list_bin(self, n): + return list(map(int, format(n, 'b').zfill(30))) + + def minFlips(self, a: int, b: int, c: int) -> int: + bin_a = self.dec_to_list_bin(a) + bin_b = self.dec_to_list_bin(b) + bin_c = self.dec_to_list_bin(c) + + ans = 0 + + for a_bit, b_bit, c_bit in zip(bin_a, bin_b, bin_c): + if (a_bit | b_bit) == c_bit: + continue + + if c_bit == 0: + ans += a_bit + b_bit + else: + ans += 1 + + return ans + + +if __name__ == '__main__': + print(Solution().minFlips(258343848, 90957776, 291428165)) + From 2c41e96c45ec2404a1cc642f86f8eb30c1e994a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Riin=E2=99=A3=EF=B8=8E?= <56533266+blaire-pi@users.noreply.github.com> Date: Thu, 10 Apr 2025 14:39:01 +0900 Subject: [PATCH 2/2] Update yerin.py --- LeetCode/1318/yerin.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/LeetCode/1318/yerin.py b/LeetCode/1318/yerin.py index 1eb5e62..76b7831 100644 --- a/LeetCode/1318/yerin.py +++ b/LeetCode/1318/yerin.py @@ -1,26 +1,17 @@ class Solution: - def dec_to_list_bin(self, n): - return list(map(int, format(n, 'b').zfill(30))) - def minFlips(self, a: int, b: int, c: int) -> int: - bin_a = self.dec_to_list_bin(a) - bin_b = self.dec_to_list_bin(b) - bin_c = self.dec_to_list_bin(c) - ans = 0 + for i in range(30): + a_bit = (a >> i) & 1 + b_bit = (b >> i) & 1 + c_bit = (c >> i) & 1 - for a_bit, b_bit, c_bit in zip(bin_a, bin_b, bin_c): if (a_bit | b_bit) == c_bit: continue if c_bit == 0: - ans += a_bit + b_bit + ans += a_bit + b_bit # 최대 2까지 가능 else: - ans += 1 + ans += 1 # c_bit == 1인데 둘 다 0이면 한 번 flip 필요 return ans - - -if __name__ == '__main__': - print(Solution().minFlips(258343848, 90957776, 291428165)) -