1007. Minimum Domino Rotations For Equal Row.cpp #119
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Approach:
We need to make all dominos show the same number on either the top or bottom row with the minimum rotations.
Since only two values are possible candidates — tops[0] or bottoms[0] — we check both.
Steps:
Let X = tops[0] and Y = bottoms[0].
Try to make all dominos equal to X (top or bottom) and also to Y.
For each domino:
If neither top nor bottom has X, then X is impossible.
Otherwise, count how many flips are needed to make all tops or all bottoms equal to X.
Do the same for Y.
The answer is the minimum flips among all valid cases, or -1 if neither works.
Intuition:
This is a brute-force check on only two candidates — the first domino’s numbers — because if a uniform value exists, it must be one of these.
We just simulate how many rotations would be needed to align all dominos to each candidate and pick the smallest valid one.
Solution in Code (C++)
class Solution {
public:
int minDominoRotations(vector& tops, vector& bottoms) {
const int n = tops.size();
};