Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1007 #1752

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,158 @@

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

**方法一:贪心**

根据题目描述,我们知道,要使得 $tops$ 中所有值或者 $bottoms$ 中所有值都相同,那么这个值必须是 $tops[0]$ 或者 $bottoms[0]$ 中的一个。

因此,我们设计一个函数 $f(x)$,表示将所有的值都变成 $x$ 的最小旋转次数,那么答案就是 $\min\{f(\textit{tops}[0]), f(\textit{bottoms}[0])\}$。

函数 $f(x)$ 的计算方法如下:

我们用两个变量 $cnt1$ 和 $cnt2$ 统计 $tops$ 和 $bottoms$ 中等于 $x$ 的个数,用 $n$ 减去它们的最大值,就是将所有值都变成 $x$ 的最小旋转次数。注意,如果 $tops$ 和 $bottoms$ 中没有等于 $x$ 的值,那么 $f(x)$ 的值就是一个很大的数,我们用 $n + 1$ 表示这个数。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
def f(x: int) -> int:
cnt1 = cnt2 = 0
for a, b in zip(tops, bottoms):
if x not in (a, b):
return inf
cnt1 += a == x
cnt2 += b == x
return len(tops) - max(cnt1, cnt2)

ans = min(f(tops[0]), f(bottoms[0]))
return -1 if ans == inf else ans
```

### **Java**

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

```java
class Solution {
private int n;
private int[] tops;
private int[] bottoms;

public int minDominoRotations(int[] tops, int[] bottoms) {
n = tops.length;
this.tops = tops;
this.bottoms = bottoms;
int ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}

private int f(int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x ? 1 : 0;
cnt2 += bottoms[i] == x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
}
}
```

### **C++**

```cpp
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int n = tops.size();
auto f = [&](int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x;
cnt2 += bottoms[i] == x;
}
return n - max(cnt1, cnt2);
};
int ans = min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
};
```

### **Go**

```go
func minDominoRotations(tops []int, bottoms []int) int {
n := len(tops)
f := func(x int) int {
cnt1, cnt2 := 0, 0
for i, a := range tops {
b := bottoms[i]
if a != x && b != x {
return n + 1
}
if a == x {
cnt1++
}
if b == x {
cnt2++
}
}
return n - max(cnt1, cnt2)
}
ans := min(f(tops[0]), f(bottoms[0]))
if ans > n {
return -1
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function minDominoRotations(tops: number[], bottoms: number[]): number {
const n = tops.length;
const f = (x: number): number => {
let [cnt1, cnt2] = [0, 0];
for (let i = 0; i < n; ++i) {
if (tops[i] !== x && bottoms[i] !== x) {
return n + 1;
}
cnt1 += tops[i] === x ? 1 : 0;
cnt2 += bottoms[i] === x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
};
const ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,154 @@ In this case, it is not possible to rotate the dominoes to make one row of value

## Solutions

**Solution 1: Greedy**

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]$.

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])\}$.

The calculation method of function $f(x)$ is as follows:

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$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
def f(x: int) -> int:
cnt1 = cnt2 = 0
for a, b in zip(tops, bottoms):
if x not in (a, b):
return inf
cnt1 += a == x
cnt2 += b == x
return len(tops) - max(cnt1, cnt2)

ans = min(f(tops[0]), f(bottoms[0]))
return -1 if ans == inf else ans
```

### **Java**

```java
class Solution {
private int n;
private int[] tops;
private int[] bottoms;

public int minDominoRotations(int[] tops, int[] bottoms) {
n = tops.length;
this.tops = tops;
this.bottoms = bottoms;
int ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}

private int f(int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x ? 1 : 0;
cnt2 += bottoms[i] == x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
}
}
```

### **C++**

```cpp
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int n = tops.size();
auto f = [&](int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x;
cnt2 += bottoms[i] == x;
}
return n - max(cnt1, cnt2);
};
int ans = min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
};
```

### **Go**

```go
func minDominoRotations(tops []int, bottoms []int) int {
n := len(tops)
f := func(x int) int {
cnt1, cnt2 := 0, 0
for i, a := range tops {
b := bottoms[i]
if a != x && b != x {
return n + 1
}
if a == x {
cnt1++
}
if b == x {
cnt2++
}
}
return n - max(cnt1, cnt2)
}
ans := min(f(tops[0]), f(bottoms[0]))
if ans > n {
return -1
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function minDominoRotations(tops: number[], bottoms: number[]): number {
const n = tops.length;
const f = (x: number): number => {
let [cnt1, cnt2] = [0, 0];
for (let i = 0; i < n; ++i) {
if (tops[i] !== x && bottoms[i] !== x) {
return n + 1;
}
cnt1 += tops[i] === x ? 1 : 0;
cnt2 += bottoms[i] === x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
};
const ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int n = tops.size();
auto f = [&](int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x;
cnt2 += bottoms[i] == x;
}
return n - max(cnt1, cnt2);
};
int ans = min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
func minDominoRotations(tops []int, bottoms []int) int {
n := len(tops)
f := func(x int) int {
cnt1, cnt2 := 0, 0
for i, a := range tops {
b := bottoms[i]
if a != x && b != x {
return n + 1
}
if a == x {
cnt1++
}
if b == x {
cnt2++
}
}
return n - max(cnt1, cnt2)
}
ans := min(f(tops[0]), f(bottoms[0]))
if ans > n {
return -1
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
Loading