← Home

1128. Number of Equivalent Domino Pairs

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
112

This is one of those problems where the clean idea matters more than the amount of code. For 1128. Number of Equivalent Domino Pairs, the solution in this repository is mainly a greedy solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are numEquivDominoPairs.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 72 ms, faster than 19.86% of C++ online submissions for Number of Equivalent Domino Pairs.
02//Memory Usage: 21.2 MB, less than 100.00% of C++ online submissions for Number of Equivalent Domino Pairs.
03class Solution {
04public:
05    int numEquivDominoPairs(vector<vector<int>>& dominoes) {
06        for(vector<int>& domino : dominoes){
07            sort(domino.begin(), domino.end());
08        }
09        sort(dominoes.begin(), dominoes.end());
10        
11        int group = 0, pair = 0;
12        bool first = true;
13        vector<int> cur = dominoes[0];
14        
15        //start from index 1!
16        for(int i = 1; i < dominoes.size(); i++){
17            if(dominoes[i] == cur){
18                group++;
19                if(first){
20                    group++;
21                    first = false;
22                }
23            }else{
24                cur = dominoes[i];
25                first = true;
26                
27                //use a new group, clean old group and update pair
28                //C n takes 2
29                pair += group*(group-1)/2;
30                group = 0;
31            }
32        }
33        pair += group*(group-1)/2;
34        
35        return pair;
36    }
37};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.