1128: Number-of-Equivalent-Domino-Pairs
Easy
You can just store the count of all the pairs in a
9x9
grid and calculate the number of pairs using the
n * (n-1) / 2
formula, where n
denotes
the number of equivalent dominoes that exist.
Code:
class Solution {
public:
int numEquivDominoPairs(vector<vector<int>>& dominoes) {
<vector<int>> grid (9, vector<int>(9, 0));
vectorfor (auto& domino : dominoes) {
++grid[domino[0]-1][domino[1]-1];
}
int ans = 0;
for (int i = 0; i < 9; ++i) {
for (int j = i; j < 9; ++j) {
int total_pairs = grid[i][j];
if (i != j) {
+= grid[j][i];
total_pairs }
+= (total_pairs-1) * (total_pairs) / 2;
ans }
}
return ans;
}
};