Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit abb6bde

Browse files
🎲 Day 19
1 parent 3ea592b commit abb6bde

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
2. [Search a 2D Matrix](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3497/) ➡️ [CPP Solution](Week3/searchMatrix.cpp)
3131
3. [Repeated DNA Sequences](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3498/) ➡️ [CPP Solution](Week3/findRepeatedDnaSequences.cpp)
3232
4. [Best Time to Buy and Sell Stock IV](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3499/) ➡️ [CPP Solution](Week3/maxProfit.cpp)
33+
5. [Minimum Domino Rotations For Equal Row](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3500/) ➡️ [CPP Solution](Week3/minDominoRotations.cpp)
3334

3435

3536
## Week 4 🚧

‎Week3/minDominoRotations.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
private:
3+
int dominoRotations(vector<int>& A, vector<int>& B) {
4+
int minSwaps = INT_MAX;
5+
int n = A.size();
6+
7+
for(int d = 1; d <= 6; ++d) {
8+
int swaps = 0;
9+
int i = 0;
10+
for(; i < n; ++i) {
11+
if(A[i] == d) continue;
12+
if(B[i] == d) swaps++;
13+
else break;
14+
}
15+
16+
if(i == n) minSwaps = min(minSwaps, swaps);
17+
swaps = 0;
18+
}
19+
20+
return minSwaps == INT_MAX ? -1 : minSwaps;
21+
}
22+
public:
23+
int minDominoRotations(vector<int>& A, vector<int>& B) {
24+
int AB = dominoRotations(A, B);
25+
int BA = dominoRotations(B, A);
26+
27+
return AB == -1 ? BA : BA == -1 ? AB : min(AB, BA);
28+
}
29+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /