Skip to content

Commit adf51d2

Browse files
authored
Update 2033-minimum-operations-to-make-a-uni-value-grid.js
1 parent a3a343c commit adf51d2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

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

0 commit comments

Comments
 (0)