Skip to content

Commit 9c95bdb

Browse files
authored
feat: add solutions to lc problem: No.1007 (#1752)
NO.1007.Minimum Domino Rotations For Equal Row
1 parent fe575a0 commit 9c95bdb

File tree

7 files changed

+384
-40
lines changed

7 files changed

+384
-40
lines changed

solution/1000-1099/1007.Minimum Domino Rotations For Equal Row/README.md

+137-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,158 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:贪心**
52+
53+
根据题目描述,我们知道,要使得 $tops$ 中所有值或者 $bottoms$ 中所有值都相同,那么这个值必须是 $tops[0]$ 或者 $bottoms[0]$ 中的一个。
54+
55+
因此,我们设计一个函数 $f(x)$,表示将所有的值都变成 $x$ 的最小旋转次数,那么答案就是 $\min\{f(\textit{tops}[0]), f(\textit{bottoms}[0])\}$。
56+
57+
函数 $f(x)$ 的计算方法如下:
58+
59+
我们用两个变量 $cnt1$ 和 $cnt2$ 统计 $tops$ 和 $bottoms$ 中等于 $x$ 的个数,用 $n$ 减去它们的最大值,就是将所有值都变成 $x$ 的最小旋转次数。注意,如果 $tops$ 和 $bottoms$ 中没有等于 $x$ 的值,那么 $f(x)$ 的值就是一个很大的数,我们用 $n + 1$ 表示这个数。
60+
61+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
62+
5163
<!-- tabs:start -->
5264

5365
### **Python3**
5466

5567
<!-- 这里可写当前语言的特殊实现逻辑 -->
5668

5769
```python
58-
70+
class Solution:
71+
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
72+
def f(x: int) -> int:
73+
cnt1 = cnt2 = 0
74+
for a, b in zip(tops, bottoms):
75+
if x not in (a, b):
76+
return inf
77+
cnt1 += a == x
78+
cnt2 += b == x
79+
return len(tops) - max(cnt1, cnt2)
80+
81+
ans = min(f(tops[0]), f(bottoms[0]))
82+
return -1 if ans == inf else ans
5983
```
6084

6185
### **Java**
6286

6387
<!-- 这里可写当前语言的特殊实现逻辑 -->
6488

6589
```java
90+
class Solution {
91+
private int n;
92+
private int[] tops;
93+
private int[] bottoms;
94+
95+
public int minDominoRotations(int[] tops, int[] bottoms) {
96+
n = tops.length;
97+
this.tops = tops;
98+
this.bottoms = bottoms;
99+
int ans = Math.min(f(tops[0]), f(bottoms[0]));
100+
return ans > n ? -1 : ans;
101+
}
102+
103+
private int f(int x) {
104+
int cnt1 = 0, cnt2 = 0;
105+
for (int i = 0; i < n; ++i) {
106+
if (tops[i] != x && bottoms[i] != x) {
107+
return n + 1;
108+
}
109+
cnt1 += tops[i] == x ? 1 : 0;
110+
cnt2 += bottoms[i] == x ? 1 : 0;
111+
}
112+
return n - Math.max(cnt1, cnt2);
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
123+
int n = tops.size();
124+
auto f = [&](int x) {
125+
int cnt1 = 0, cnt2 = 0;
126+
for (int i = 0; i < n; ++i) {
127+
if (tops[i] != x && bottoms[i] != x) {
128+
return n + 1;
129+
}
130+
cnt1 += tops[i] == x;
131+
cnt2 += bottoms[i] == x;
132+
}
133+
return n - max(cnt1, cnt2);
134+
};
135+
int ans = min(f(tops[0]), f(bottoms[0]));
136+
return ans > n ? -1 : ans;
137+
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
func minDominoRotations(tops []int, bottoms []int) int {
145+
n := len(tops)
146+
f := func(x int) int {
147+
cnt1, cnt2 := 0, 0
148+
for i, a := range tops {
149+
b := bottoms[i]
150+
if a != x && b != x {
151+
return n + 1
152+
}
153+
if a == x {
154+
cnt1++
155+
}
156+
if b == x {
157+
cnt2++
158+
}
159+
}
160+
return n - max(cnt1, cnt2)
161+
}
162+
ans := min(f(tops[0]), f(bottoms[0]))
163+
if ans > n {
164+
return -1
165+
}
166+
return ans
167+
}
168+
169+
func max(a, b int) int {
170+
if a > b {
171+
return a
172+
}
173+
return b
174+
}
175+
176+
func min(a, b int) int {
177+
if a < b {
178+
return a
179+
}
180+
return b
181+
}
182+
```
66183

184+
### **TypeScript**
185+
186+
```ts
187+
function minDominoRotations(tops: number[], bottoms: number[]): number {
188+
const n = tops.length;
189+
const f = (x: number): number => {
190+
let [cnt1, cnt2] = [0, 0];
191+
for (let i = 0; i < n; ++i) {
192+
if (tops[i] !== x && bottoms[i] !== x) {
193+
return n + 1;
194+
}
195+
cnt1 += tops[i] === x ? 1 : 0;
196+
cnt2 += bottoms[i] === x ? 1 : 0;
197+
}
198+
return n - Math.max(cnt1, cnt2);
199+
};
200+
const ans = Math.min(f(tops[0]), f(bottoms[0]));
201+
return ans > n ? -1 : ans;
202+
}
67203
```
68204

69205
### **...**

solution/1000-1099/1007.Minimum Domino Rotations For Equal Row/README_EN.md

+137-1
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,154 @@ In this case, it is not possible to rotate the dominoes to make one row of value
4343

4444
## Solutions
4545

46+
**Solution 1: Greedy**
47+
48+
According to the problem description, we know that in order to make all values in $tops$ or all values in $bottoms$ the same, the value must be one of $tops[0]$ or $bottoms[0]$.
49+
50+
Therefore, we design a function $f(x)$ to represent the minimum number of rotations required to make all values equal to $x$. Then the answer is $\min\{f(\textit{tops}[0]), f(\textit{bottoms}[0])\}$.
51+
52+
The calculation method of function $f(x)$ is as follows:
53+
54+
We use two variables $cnt1$ and $cnt2$ to count the number of occurrences of $x$ in $tops$ and $bottoms$, respectively. We subtract the maximum value of $cnt1$ and $cnt2$ from $n$, which is the minimum number of rotations required to make all values equal to $x$. Note that if there are no values equal to $x$ in $tops$ and $bottoms$, the value of $f(x)$ is a very large number, which we represent as $n+1$.
55+
56+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**
4961

5062
```python
51-
63+
class Solution:
64+
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
65+
def f(x: int) -> int:
66+
cnt1 = cnt2 = 0
67+
for a, b in zip(tops, bottoms):
68+
if x not in (a, b):
69+
return inf
70+
cnt1 += a == x
71+
cnt2 += b == x
72+
return len(tops) - max(cnt1, cnt2)
73+
74+
ans = min(f(tops[0]), f(bottoms[0]))
75+
return -1 if ans == inf else ans
5276
```
5377

5478
### **Java**
5579

5680
```java
81+
class Solution {
82+
private int n;
83+
private int[] tops;
84+
private int[] bottoms;
85+
86+
public int minDominoRotations(int[] tops, int[] bottoms) {
87+
n = tops.length;
88+
this.tops = tops;
89+
this.bottoms = bottoms;
90+
int ans = Math.min(f(tops[0]), f(bottoms[0]));
91+
return ans > n ? -1 : ans;
92+
}
93+
94+
private int f(int x) {
95+
int cnt1 = 0, cnt2 = 0;
96+
for (int i = 0; i < n; ++i) {
97+
if (tops[i] != x && bottoms[i] != x) {
98+
return n + 1;
99+
}
100+
cnt1 += tops[i] == x ? 1 : 0;
101+
cnt2 += bottoms[i] == x ? 1 : 0;
102+
}
103+
return n - Math.max(cnt1, cnt2);
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
114+
int n = tops.size();
115+
auto f = [&](int x) {
116+
int cnt1 = 0, cnt2 = 0;
117+
for (int i = 0; i < n; ++i) {
118+
if (tops[i] != x && bottoms[i] != x) {
119+
return n + 1;
120+
}
121+
cnt1 += tops[i] == x;
122+
cnt2 += bottoms[i] == x;
123+
}
124+
return n - max(cnt1, cnt2);
125+
};
126+
int ans = min(f(tops[0]), f(bottoms[0]));
127+
return ans > n ? -1 : ans;
128+
}
129+
};
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func minDominoRotations(tops []int, bottoms []int) int {
136+
n := len(tops)
137+
f := func(x int) int {
138+
cnt1, cnt2 := 0, 0
139+
for i, a := range tops {
140+
b := bottoms[i]
141+
if a != x && b != x {
142+
return n + 1
143+
}
144+
if a == x {
145+
cnt1++
146+
}
147+
if b == x {
148+
cnt2++
149+
}
150+
}
151+
return n - max(cnt1, cnt2)
152+
}
153+
ans := min(f(tops[0]), f(bottoms[0]))
154+
if ans > n {
155+
return -1
156+
}
157+
return ans
158+
}
159+
160+
func max(a, b int) int {
161+
if a > b {
162+
return a
163+
}
164+
return b
165+
}
166+
167+
func min(a, b int) int {
168+
if a < b {
169+
return a
170+
}
171+
return b
172+
}
173+
```
57174

175+
### **TypeScript**
176+
177+
```ts
178+
function minDominoRotations(tops: number[], bottoms: number[]): number {
179+
const n = tops.length;
180+
const f = (x: number): number => {
181+
let [cnt1, cnt2] = [0, 0];
182+
for (let i = 0; i < n; ++i) {
183+
if (tops[i] !== x && bottoms[i] !== x) {
184+
return n + 1;
185+
}
186+
cnt1 += tops[i] === x ? 1 : 0;
187+
cnt2 += bottoms[i] === x ? 1 : 0;
188+
}
189+
return n - Math.max(cnt1, cnt2);
190+
};
191+
const ans = Math.min(f(tops[0]), f(bottoms[0]));
192+
return ans > n ? -1 : ans;
193+
}
58194
```
59195

60196
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
4+
int n = tops.size();
5+
auto f = [&](int x) {
6+
int cnt1 = 0, cnt2 = 0;
7+
for (int i = 0; i < n; ++i) {
8+
if (tops[i] != x && bottoms[i] != x) {
9+
return n + 1;
10+
}
11+
cnt1 += tops[i] == x;
12+
cnt2 += bottoms[i] == x;
13+
}
14+
return n - max(cnt1, cnt2);
15+
};
16+
int ans = min(f(tops[0]), f(bottoms[0]));
17+
return ans > n ? -1 : ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
func minDominoRotations(tops []int, bottoms []int) int {
2+
n := len(tops)
3+
f := func(x int) int {
4+
cnt1, cnt2 := 0, 0
5+
for i, a := range tops {
6+
b := bottoms[i]
7+
if a != x && b != x {
8+
return n + 1
9+
}
10+
if a == x {
11+
cnt1++
12+
}
13+
if b == x {
14+
cnt2++
15+
}
16+
}
17+
return n - max(cnt1, cnt2)
18+
}
19+
ans := min(f(tops[0]), f(bottoms[0]))
20+
if ans > n {
21+
return -1
22+
}
23+
return ans
24+
}
25+
26+
func max(a, b int) int {
27+
if a > b {
28+
return a
29+
}
30+
return b
31+
}
32+
33+
func min(a, b int) int {
34+
if a < b {
35+
return a
36+
}
37+
return b
38+
}

0 commit comments

Comments
 (0)