Skip to content

Commit 1443a2d

Browse files
authored
Update 2033-minimum-operations-to-make-a-uni-value-grid.js
1 parent 6fb1bbc commit 1443a2d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2033-minimum-operations-to-make-a-uni-value-grid.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @param {number} x
4+
* @return {number}
5+
*/
6+
const minOperations = function (grid, x) {
7+
const arr = [],
8+
m = grid.length,
9+
n = grid[0].length
10+
const len = m * n
11+
for (let i = 0; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
arr.push(grid[i][j])
14+
}
15+
}
16+
arr.sort((a, b) => a - b)
17+
const mid = arr[~~(len / 2)], { abs } = Math
18+
let res = 0
19+
for(const e of arr) {
20+
if(abs(mid - e) % x !== 0) return -1
21+
res += abs(mid - e) / x
22+
}
23+
24+
return res
25+
}
26+
27+
// another
28+
129
/**
230
* @param {number[][]} grid
331
* @param {number} x

0 commit comments

Comments
 (0)