1007: Minimum-Domino-Rotations-For-Equal-Row
Medium
This question is weird. I dislike my solution, but I will post about it anyways because I alksjdaslkjdlaskjd.
Anyways, basically my algo takes the first domino and creates a
hashmap using tops[0]
and
bottoms[0]
as keys, where in the
[key, value]
pair, the value
is a
pair<int, int>
structure with
value.first
storing the tops
count and
value.second
storing the bottoms
count.
It then just iterates through all the dominoes and checks if either keys are present on either side of the domino.
Afterwards,
Code:
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
<int, pair<int, int>> mp;
unordered_map++mp[tops[0]].first;
++mp[bottoms[0]].second;
for (int i = 1; i < tops.size(); ++i) {
if (mp.size() == 0) break;
<int> erase;
vectorfor (auto& [key, value] : mp) {
if (key == tops[i] || key == bottoms[i]) {
if (key == tops[i]) {
++value.first;
}
if (key == bottoms[i]) {
++value.second;
}
} else {
.push_back(key);
erase}
}
for (int& n : erase) {
.erase(n);
mp}
}
if (mp.size() == 0) {
return -1;
}
int ans = INT32_MAX;
for (auto& [key, value] : mp) {
= min(ans, (int)min(tops.size()-value.first, tops.size()-value.second));
ans }
return ans;
}
};