diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/1.png b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/1.png new file mode 100644 index 000000000..9d39df42a Binary files /dev/null and b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/1.png differ diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/2.png b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/2.png new file mode 100644 index 000000000..4a23797c8 Binary files /dev/null and b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/2.png differ diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/3.png b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/3.png new file mode 100644 index 000000000..e301c5cac Binary files /dev/null and b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/3.png differ diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/README.md b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/README.md index 92d48a887..0dc9bdea8 100755 --- a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/README.md +++ b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/README.md @@ -1,28 +1,45 @@ # [2033.Minimum Operations to Make a Uni-Value Grid][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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`. + +A **uni-value** grid is a grid where all the elements of it are equal. + +Return the **minimum** number of operations to make the grid **uni-value**. If it is not possible, return `-1`. -**Example 1:** +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: grid = [[2,4],[6,8]], x = 2 +Output: 4 +Explanation: We can make every element equal to 4 by doing the following: +- Add x to 2 once. +- Subtract x from 6 once. +- Subtract x from 8 twice. +A total of 4 operations were used. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Minimum Operations to Make a Uni-Value Grid -```go +``` +Input: grid = [[1,5],[2,3]], x = 1 +Output: 5 +Explanation: We can make every element equal to 3. ``` +**Example 3:** + +![3](./3.png) + +``` +Input: grid = [[1,2],[3,4]], x = 2 +Output: -1 +Explanation: It is impossible to make every element equal. +``` ## 结语 diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution.go b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution.go index d115ccf5e..529984641 100644 --- a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution.go +++ b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(grid [][]int, x int) int { + list := []int{} + rows, cols := len(grid), len(grid[0]) + for i := range rows { + for j := range cols { + list = append(list, grid[i][j]) + } + } + sort.Ints(list) + mid := list[len(list)/2] + ans := 0 + for _, n := range list { + if n%x != mid%x { + return -1 + } + diff := n - mid + if diff < 0 { + diff = -diff + } + ans += diff / x + } + return ans } diff --git a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution_test.go b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution_test.go index 14ff50eb4..11ec392b2 100644 --- a/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution_test.go +++ b/leetcode/2001-2100/2033.Minimum-Operations-to-Make-a-Uni-Value-Grid/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + grid [][]int + x int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{2, 4}, {6, 8}}, 2, 4}, + {"TestCase2", [][]int{{1, 5}, {2, 3}}, 1, 5}, + {"TestCase3", [][]int{{1, 2}, {3, 4}}, 2, -1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.grid, c.x) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.grid, c.x) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }