Skip to content

Commit 05164d6

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (99.71%), Memory - 24.3 MB (81.31%)
1 parent 5fe3c80 commit 05164d6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<p>Given a list of <code>dominoes</code>, <code>dominoes[i] = [a, b]</code> is <strong>equivalent to</strong> <code>dominoes[j] = [c, d]</code> if and only if either (<code>a == c</code> and <code>b == d</code>), or (<code>a == d</code> and <code>b == c</code>) - that is, one domino can be rotated to be equal to another domino.</p>
2+
3+
<p>Return <em>the number of pairs </em><code>(i, j)</code><em> for which </em><code>0 &lt;= i &lt; j &lt; dominoes.length</code><em>, and </em><code>dominoes[i]</code><em> is <strong>equivalent to</strong> </em><code>dominoes[j]</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> dominoes = [[1,2],[2,1],[3,4],[5,6]]
10+
<strong>Output:</strong> 1
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
17+
<strong>Output:</strong> 3
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= dominoes.length &lt;= 4 * 10<sup>4</sup></code></li>
25+
<li><code>dominoes[i].length == 2</code></li>
26+
<li><code>1 &lt;= dominoes[i][j] &lt;= 9</code></li>
27+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Approach: Tuple Representation + Counting
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
8+
num = [0] * 100
9+
result = 0
10+
11+
for x, y in dominoes:
12+
val = x * 10 + y if x <= y else y * 10 + x
13+
result += num[val]
14+
num[val] += 1
15+
16+
return result

0 commit comments

Comments
 (0)