-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04_Number_of_Equivalent_Domino_Pairs.cpp
More file actions
50 lines (39 loc) · 1.5 KB
/
Copy path04_Number_of_Equivalent_Domino_Pairs.cpp
File metadata and controls
50 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 1128. Number of Equivalent Domino Pairs
// Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino.
// Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
// Example 1:
// Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
// Output: 1
// Example 2:
// Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
// Output: 3
// Constraints:
// 1 <= dominoes.length <= 4 * 104
// dominoes[i].length == 2
// 1 <= dominoes[i][j] <= 9
class Solution
{
public:
int numEquivDominoPairs(vector<vector<int>> &dominoes)
{
int count = 0;
unordered_map<int, int> m;
for (auto d : dominoes)
{
int key = min(d[0], d[1]) * 10 + max(d[0], d[1]);
count += m[key];
m[key]++;
}
return count;
}
};
/*
This code finds the number of equivalent domino pairs in a given array of dominoes.
For each domino [a,b], it creates a unique key by taking min(a,b)*10 + max(a,b).
This ensures that [1,2] and [2,1] will have the same key (12).
The unordered_map 'm' keeps track of frequency of each key.
For each domino, we add the current frequency of its key to count (pairs with previous dominoes)
and then increment its frequency.
Time Complexity: O(n) where n is number of dominoes
Space Complexity: O(n) for storing the frequency map
*/