Skip to content

Commit 4eacdff

Browse files
committed
Sync LeetCode submission Runtime - 24 ms (88.63%), Memory - 18.4 MB (67.37%)
1 parent b3ed3bb commit 4eacdff

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>In a row of dominoes, <code>tops[i]</code> and <code>bottoms[i]</code> represent the top and bottom halves of the <code>i<sup>th</sup></code> domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)</p>
2+
3+
<p>We may rotate the <code>i<sup>th</sup></code> domino, so that <code>tops[i]</code> and <code>bottoms[i]</code> swap values.</p>
4+
5+
<p>Return the minimum number of rotations so that all the values in <code>tops</code> are the same, or all the values in <code>bottoms</code> are the same.</p>
6+
7+
<p>If it cannot be done, return <code>-1</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/14/domino.png" style="height: 300px; width: 421px;" />
12+
<pre>
13+
<strong>Input:</strong> tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
14+
<strong>Output:</strong> 2
15+
<strong>Explanation:</strong>
16+
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
17+
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
24+
<strong>Output:</strong> -1
25+
<strong>Explanation:</strong>
26+
In this case, it is not possible to rotate the dominoes to make one row of values equal.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>2 &lt;= tops.length &lt;= 2 * 10<sup>4</sup></code></li>
34+
<li><code>bottoms.length == tops.length</code></li>
35+
<li><code>1 &lt;= tops[i], bottoms[i] &lt;= 6</code></li>
36+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Approach: Greedy
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
8+
n = len(tops)
9+
10+
def check(x):
11+
# Count rotations needed to make all tops x
12+
rotations_top = 0
13+
14+
# Count rotations needed to make all bottoms x
15+
rotations_bottom = 0
16+
17+
for i in range(n):
18+
# If neither top nor bottom is x, impossible
19+
if tops[i] != x and bottoms[i] != x:
20+
return float('inf')
21+
# If top is not x but bottom is x
22+
elif tops[i] != x:
23+
rotations_top += 1
24+
# If bottom is not x but top is x
25+
elif bottoms[i] != x:
26+
rotations_bottom += 1
27+
28+
return min(rotations_top, rotations_bottom)
29+
30+
# Try making everything the value of first domino's bottom or first domino's bottom
31+
min_rotations = min(check(tops[0]), check(bottoms[0]))
32+
33+
return -1 if min_rotations == float('inf') else min_rotations

0 commit comments

Comments
 (0)