-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (100 loc) · 2.55 KB
/
main.go
File metadata and controls
108 lines (100 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Source: https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid
// Title: Minimum Operations to Make a Uni-Value Grid
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:**
// https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png
//
// ```
// 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:**
// https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png
//
// ```
// Input: grid = [[1,5],[2,3]], x = 1
// Output: 5
// Explanation: We can make every element equal to 3.
// ```
//
// **Example 3:**
// https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png
//
// ```
// Input: grid = [[1,2],[3,4]], x = 2
// Output: -1
// Explanation: It is impossible to make every element equal.
// ```
//
// **Constraints:**
//
// - `m == grid.length`
// - `n == grid[i].length`
// - `1 <= m, n <= 10^5`
// - `1 <= m * n <= 10^5`
// - `1 <= x, grid[i][j] <= 10^4`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"slices"
)
// Minimize |a_1-k| + ... + |a_n-k|
// Say a_(t-1) <= k < a_t
// = (k-a_1) + ... + (k-a_(t-1)) + (a_t-k) + ... + (a_n-k)
// = sum(a_i) - 2*(a_1+...+a_(t-1)) - (n-2t)*k
func minOperations(grid [][]int, x int) int {
m, n := len(grid), len(grid[0])
mn := m * n
nums := make([]int, mn)
r := grid[0][0] % x
sum := 0
for i := range mn {
num := grid[i/n][i%n]
if num%x != r {
return -1
}
nums[i] = num
sum += num
}
slices.Sort(nums)
minVal := sum
presum, t := 0, 0
for k := nums[0]; k <= nums[mn-1]; k += x {
for t < mn && k >= nums[t] {
presum += nums[t]
t++
}
val := sum - 2*presum - (mn-2*t)*k
if val < minVal {
minVal = val
} else {
break
}
}
return minVal / x
}
func main() {
fmt.Println(minOperations(
[][]int{
{1, 2, 3},
{4, 5, 6},
},
1,
))
}