Skip to content

Commit 1bf0fe3

Browse files
authored
feat: add solutions to lc problem: No.1007 (#4384)
No.1007.Minimum Domino Rotations For Equal Row
1 parent bed93c3 commit 1bf0fe3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,35 @@ function minDominoRotations(tops: number[], bottoms: number[]): number {
199199
}
200200
```
201201

202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn min_domino_rotations(tops: Vec<i32>, bottoms: Vec<i32>) -> i32 {
207+
let n = tops.len() as i32;
208+
let f = |x: i32| -> i32 {
209+
let mut cnt1 = 0;
210+
let mut cnt2 = 0;
211+
for i in 0..n as usize {
212+
if tops[i] != x && bottoms[i] != x {
213+
return n + 1;
214+
}
215+
if tops[i] == x {
216+
cnt1 += 1;
217+
}
218+
if bottoms[i] == x {
219+
cnt2 += 1;
220+
}
221+
}
222+
n - cnt1.max(cnt2)
223+
};
224+
225+
let ans = f(tops[0]).min(f(bottoms[0]));
226+
if ans > n { -1 } else { ans }
227+
}
228+
}
229+
```
230+
202231
<!-- tabs:end -->
203232

204233
<!-- solution:end -->

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

+29
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ function minDominoRotations(tops: number[], bottoms: number[]): number {
198198
}
199199
```
200200

201+
#### Rust
202+
203+
```rust
204+
impl Solution {
205+
pub fn min_domino_rotations(tops: Vec<i32>, bottoms: Vec<i32>) -> i32 {
206+
let n = tops.len() as i32;
207+
let f = |x: i32| -> i32 {
208+
let mut cnt1 = 0;
209+
let mut cnt2 = 0;
210+
for i in 0..n as usize {
211+
if tops[i] != x && bottoms[i] != x {
212+
return n + 1;
213+
}
214+
if tops[i] == x {
215+
cnt1 += 1;
216+
}
217+
if bottoms[i] == x {
218+
cnt2 += 1;
219+
}
220+
}
221+
n - cnt1.max(cnt2)
222+
};
223+
224+
let ans = f(tops[0]).min(f(bottoms[0]));
225+
if ans > n { -1 } else { ans }
226+
}
227+
}
228+
```
229+
201230
<!-- tabs:end -->
202231

203232
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn min_domino_rotations(tops: Vec<i32>, bottoms: Vec<i32>) -> i32 {
3+
let n = tops.len() as i32;
4+
let f = |x: i32| -> i32 {
5+
let mut cnt1 = 0;
6+
let mut cnt2 = 0;
7+
for i in 0..n as usize {
8+
if tops[i] != x && bottoms[i] != x {
9+
return n + 1;
10+
}
11+
if tops[i] == x {
12+
cnt1 += 1;
13+
}
14+
if bottoms[i] == x {
15+
cnt2 += 1;
16+
}
17+
}
18+
n - cnt1.max(cnt2)
19+
};
20+
21+
let ans = f(tops[0]).min(f(bottoms[0]));
22+
if ans > n {
23+
-1
24+
} else {
25+
ans
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)