aboutsummaryrefslogtreecommitdiff
path: root/min_domino_rotation_eq_row.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-05-03 01:12:52 -0600
committerJack Sangdahl <[email protected]>2025-05-03 01:12:52 -0600
commitd200dac0fb0717dec93bfad66d467f7dccf1b049 (patch)
treef9f4b2f865ce9b8bfb141ff3888e4583bcdaf569 /min_domino_rotation_eq_row.py
parente820af62f134a5cfd0a02b583d24207af3379b08 (diff)
downloadleetcode-d200dac0fb0717dec93bfad66d467f7dccf1b049.tar.gz
leetcode-d200dac0fb0717dec93bfad66d467f7dccf1b049.zip
1007. minimum domino rotations for equal row
Diffstat (limited to 'min_domino_rotation_eq_row.py')
-rw-r--r--min_domino_rotation_eq_row.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/min_domino_rotation_eq_row.py b/min_domino_rotation_eq_row.py
new file mode 100644
index 0000000..03766f4
--- /dev/null
+++ b/min_domino_rotation_eq_row.py
@@ -0,0 +1,30 @@
+# 1007. Minimum Domino Rotations For Equal Row
+from functools import reduce
+
+"""
+in a row of dominoes, 'tops[i]' and 'bottoms[i]' represents the top and
+bottom halves of the i'th domino. we may rotate the i'th domino so that
+'tops[i]' and 'bottoms[i]' swap values. return the minimum number of
+rotations so that all values in 'tops' are the same, or all values in
+'bottoms' are the same. if it cannot be done, return -1.
+"""
+
+
+class Solution(object):
+ def minDominoRotations(self, tops, bottoms):
+ """
+ :type tops: List[int]
+ :type bottoms: List[int]
+ :rtype: int
+ """
+ s = reduce(set.__and__, (set(i) for i in zip(tops, bottoms)))
+ if not s:
+ return -1
+ x = s.pop()
+ return min(len(tops) - tops.count(x), len(bottoms) - bottoms.count(x))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minDominoRotations(tops=[2, 1, 2, 4, 2, 2], bottoms=[5, 2, 6, 2, 3, 2]))
+ print(obj.minDominoRotations(tops=[3, 5, 1, 2, 3], bottoms=[3, 6, 3, 3, 4]))