aboutsummaryrefslogtreecommitdiff
path: root/min_bit_flips_convert_num.py
diff options
context:
space:
mode:
Diffstat (limited to 'min_bit_flips_convert_num.py')
-rw-r--r--min_bit_flips_convert_num.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/min_bit_flips_convert_num.py b/min_bit_flips_convert_num.py
new file mode 100644
index 0000000..6b5da6f
--- /dev/null
+++ b/min_bit_flips_convert_num.py
@@ -0,0 +1,24 @@
+# 2220. Minimum Bit Flips to Convert Number
+
+"""
+a bit flip of a number 'x' is choosing a bit in the binary representation of
+'x' and flipping it from either 0 to 1 or 1 to 0. given two integers 'start'
+and 'goal', return the minimum number of bit flips to convert 'start' to
+'goal'.
+"""
+
+
+class Solution(object):
+ def minBitFlips(self, start, goal):
+ """
+ :type start: int
+ :type goal: int
+ :rtype: int
+ """
+ return (start ^ goal).bit_count()
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minBitFlips(10, 7))
+ print(obj.minBitFlips(3, 4))