← Home

1007. Minimum Domino Rotations For Equal Row

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
100

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1007. Minimum Domino Rotations For Equal Row, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. 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: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • WA
  • 78 / 84 test cases passed.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minDominoRotations, countA.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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), space: O(N)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//WA
02//78 / 84 test cases passed.
03class Solution {
04public:
05    int minDominoRotations(vector<int>& A, vector<int>& B) {
06        int aValue = A[0], bValue = B[0];
07        int N = A.size();
08        int aSwap = 0, bSwap = 0;
09        
10        for(int i = 1; i < N; i++){
11            if(A[i] == aValue){
12                continue;
13            }else if(B[i] == aValue){
14                aSwap++;
15            }else{
16                //cannot swap so that A is filled with aValue
17                aSwap = INT_MAX;
18                break;
19            }
20        }
21        
22        for(int i = 1; i < N; i++){
23            if(B[i] == bValue){
24                continue;
25            }else if(A[i] == bValue){
26                bSwap++;
27            }else{
28                bSwap = INT_MAX;
29                break;
30            }
31        }
32        
33        cout << aSwap << " " << bSwap << endl;
34        if(aSwap == INT_MAX && bSwap == INT_MAX) return -1;
35        return min(aSwap, bSwap);
36    }
37};
38
39//https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas
40//Runtime: 128 ms, faster than 42.29% of C++ online submissions for Minimum Domino Rotations For Equal Row.
41//Memory Usage: 17.1 MB, less than 100.00% of C++ online submissions for Minimum Domino Rotations For Equal Row.
42//time: O(N), space: O(N)
43class Solution {
44public:
45    int minDominoRotations(vector<int>& A, vector<int>& B) {
46        int N = A.size();
47        vector<int> countA(7, 0), countB(7, 0), same(7, 0);
48        for(int i = 0; i < N; i++){
49            countA[A[i]]++;
50            countB[B[i]]++;
51            if(A[i] == B[i]) same[A[i]]++;
52        }
53        
54        int ans = -1;
55        for(int i = 1; i <= 6; i++){
56            /*
57            countA[i] + countB[i] - same[i]: i can fill how many position,
58            this value will always <= N
59            */
60            if(countA[i] + countB[i] - same[i] == N){
61                /*
62                if countA[i] > countB[i],
63                we flip N - countA[i] times so that 
64                row A will be all i
65                */
66                ans = N - max(countA[i], countB[i]);
67                /*
68                we can break the loop immediately because when there are 2 "i"
69                (say x and y) meets the condition 
70                "countA[i] + countB[i] - same[i] == N", 
71                same[x] and same[y] will be 0, 
72                and countA[x] will equal countB[y], 
73                countA[y] will equal countB[x],
74                so max(countA[i], countB[i]) and so ans will be the same
75                */
76                break;
77            }
78            // cout << i << " " << countA[i] << " " << countB[i] << " " << same[i] << " " << ans << endl;
79        }
80        
81        return ans;
82    }
83};
84
85//Two pass
86//https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas
87//Runtime: 120 ms, faster than 83.05% of C++ online submissions for Minimum Domino Rotations For Equal Row.
88//Memory Usage: 16.7 MB, less than 100.00% of C++ online submissions for Minimum Domino Rotations For Equal Row.
89//time: O(N), space: O(1)
90class Solution {
91public:
92    int minDominoRotations(vector<int>& A, vector<int>& B) {
93        int N = A.size();
94        int aSwap = 0, bSwap = 0;
95        
96        //try make row A(maintained by aSwap) or row B(aSwap) all A[0]
97        for(int i = 0; i < N; i++){
98            if(A[i] == A[0] || B[i] == A[0]){
99                if(A[i] != A[0]) aSwap++;
100                if(B[i] != A[0]) bSwap++;
101            }else{
102                /*
103                for position i, 
104                we cannot swap the element in A or B to make it A[0],
105                so we fail it
106                */
107                break;
108            }
109            /*
110            the condition: A[i] == A[0] || B[i] == A[0] holds for all i
111            now we can determine whether to make row A or row B all A[0]
112            */
113            if(i == N-1) return min(aSwap, bSwap);
114        }
115        
116        //try make row A or row B all B[0]
117        aSwap = 0, bSwap = 0;
118        for(int i = 0; i < N; i++){
119            if(A[i] == B[0] || B[i] == B[0]){
120                if(A[i] != B[0]) aSwap++;
121                if(B[i] != B[0]) bSwap++;
122            }else{
123                break;
124            }
125            if(i == N-1) return min(aSwap, bSwap);
126        }
127        
128        return -1;
129    }
130};
131
132//Set
133//https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas
134//Runtime: 732 ms, faster than 5.03% of C++ online submissions for Minimum Domino Rotations For Equal Row.
135//Memory Usage: 114.6 MB, less than 16.67% of C++ online submissions for Minimum Domino Rotations For Equal Row.
136class Solution {
137public:
138    int minDominoRotations(vector<int>& A, vector<int>& B) {
139        set<int> mainset = {1,2,3,4,5,6};
140        vector<int> countA(7, 0), countB(7, 0);
141        int N = A.size();
142        
143        for (int i = 0; i < N; ++i) {
144            set<int> tmpset = {A[i], B[i]};
145            set<int> result;
146            set_intersection(mainset.begin(), mainset.end(), 
147                                 tmpset.begin(), tmpset.end(),
148                                 inserter(result, result.begin()));
149            mainset.swap(result);
150            countA[A[i]]++;
151            countB[B[i]]++;
152        }
153        
154        //here we get the value either exist in row A or B for every position
155        // for(int i : mainset){
156        //     cout << i << " ";
157        // }
158        // cout << endl;
159        
160        /*
161        if there are 2 values in mainset, for example:
162        [2,1,2,1,2,2]
163        [1,2,1,2,1,1]
164        their max(countA[i], countB[i]) will be the same,
165        so we can jsut calculate N - max(countA[i], countB[i]) for one value and return
166        */
167        for(int i : mainset){
168            return N - max(countA[i], countB[i]);
169        }
170        return -1;
171    }
172};

Cost

Complexity

Time
O(N), space: O(N)
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.