Skip to content

Commit 2f78d01

Browse files
authored
Merge pull request #1159 from 0xff-dev/2033
Add solution and test-cases for problem 2033
2 parents 60d171f + 46ad1ab commit 2f78d01

File tree

6 files changed

+65
-25
lines changed

6 files changed

+65
-25
lines changed
Loading
Loading
Loading

leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/README.md

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [2033.Minimum Operations to Make a Uni-Value Grid][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+
You are given a 2D integer `grid` of size `m x n` and an integer `x`. In one operation, you can **add** `x` to or **subtract** `x` from any element in the `grid`.
5+
6+
A **uni-value** grid is a grid where all the elements of it are equal.
7+
8+
Return the **minimum** number of operations to make the grid **uni-value**. If it is not possible, return `-1`.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: grid = [[2,4],[6,8]], x = 2
16+
Output: 4
17+
Explanation: We can make every element equal to 4 by doing the following:
18+
- Add x to 2 once.
19+
- Subtract x from 6 once.
20+
- Subtract x from 8 twice.
21+
A total of 4 operations were used.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./2.png)
1927

20-
### 思路1
21-
> ...
22-
Minimum Operations to Make a Uni-Value Grid
23-
```go
28+
```
29+
Input: grid = [[1,5],[2,3]], x = 1
30+
Output: 5
31+
Explanation: We can make every element equal to 3.
2432
```
2533

34+
**Example 3:**
35+
36+
![3](./3.png)
37+
38+
```
39+
Input: grid = [[1,2],[3,4]], x = 2
40+
Output: -1
41+
Explanation: It is impossible to make every element equal.
42+
```
2643

2744
## 结语
2845

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(grid [][]int, x int) int {
6+
list := []int{}
7+
rows, cols := len(grid), len(grid[0])
8+
for i := range rows {
9+
for j := range cols {
10+
list = append(list, grid[i][j])
11+
}
12+
}
13+
sort.Ints(list)
14+
mid := list[len(list)/2]
15+
ans := 0
16+
for _, n := range list {
17+
if n%x != mid%x {
18+
return -1
19+
}
20+
diff := n - mid
21+
if diff < 0 {
22+
diff = -diff
23+
}
24+
ans += diff / x
25+
}
26+
return ans
527
}

leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution_test.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
grid [][]int
14+
x int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{2, 4}, {6, 8}}, 2, 4},
18+
{"TestCase2", [][]int{{1, 5}, {2, 3}}, 1, 5},
19+
{"TestCase3", [][]int{{1, 2}, {3, 4}}, 2, -1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.grid, c.x)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.grid, c.x)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)