Skip to content

Commit 72e1245

Browse files
committed
Add solution and test-cases for problem 1007
1 parent af242e1 commit 72e1245

File tree

4 files changed

+73
-26
lines changed

4 files changed

+73
-26
lines changed

leetcode/1001-1100/1007.Minimum-Domino-Rotations-For-Equal-Row/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1007.Minimum Domino Rotations For Equal Row][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
In a row of dominoes, `tops[i]` and `bottoms[i]` represent the top and bottom halves of the __i<sup>th</sup>__ domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
75

8-
**Example 1:**
6+
We may rotate the __i<sup>th</sup>__ domino, so that `tops[i]` and `bottoms[i]` swap values.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Return the minimum number of rotations so that all the values in `tops` are the same, or all the values in `bottoms` are the same.
149

15-
## 题意
16-
> ...
10+
If it cannot be done, return `-1`.
1711

18-
## 题解
12+
**Example 1:**
13+
![domino](./domino.png)
1914

20-
### 思路1
21-
> ...
22-
Minimum Domino Rotations For Equal Row
23-
```go
15+
```
16+
Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
17+
Output: 2
18+
Explanation:
19+
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
20+
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
2421
```
2522

23+
**Example 2:**
24+
```
25+
Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
26+
Output: -1
27+
Explanation:
28+
In this case, it is not possible to rotate the dominoes to make one row of values equal.
29+
```
2630

2731
## 结语
2832

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func topCanImpl(elem int, tops, bottoms []int) (bool, int) {
4+
count := 0
5+
for idx := 1; idx < len(tops); idx++ {
6+
if tops[idx] == elem {
7+
continue
8+
}
9+
if bottoms[idx] != elem {
10+
return false, count
11+
}
12+
count++
13+
}
14+
15+
return true, count
16+
}
17+
18+
func bottomCanImpl(elem int, tops, bottoms []int) (bool, int) {
19+
count := 0
20+
for idx := 1; idx < len(bottoms); idx++ {
21+
if bottoms[idx] == elem {
22+
continue
23+
}
24+
if tops[idx] != elem {
25+
return false, count
26+
}
27+
count++
28+
}
29+
30+
return true, count
31+
}
32+
33+
func Solution(tops []int, bottoms []int) int {
34+
minCount := -1
35+
if ttOk, ttCount := topCanImpl(tops[0], tops, bottoms); ttOk && (minCount == -1 || minCount > ttCount) {
36+
minCount = ttCount
37+
}
38+
if tbOk, tbCount := topCanImpl(bottoms[0], tops, bottoms); tbOk && (minCount == -1 || minCount > tbCount+1) {
39+
minCount = tbCount + 1
40+
}
41+
if bbOk, bbCount := bottomCanImpl(bottoms[0], tops, bottoms); bbOk && (minCount == -1 || minCount > bbCount) {
42+
minCount = bbCount
43+
}
44+
if btOk, btCount := bottomCanImpl(tops[0], tops, bottoms); btOk && (minCount == -1 || minCount > btCount+1) {
45+
minCount = btCount + 1
46+
}
47+
return minCount
548
}

0 commit comments

Comments
 (0)