Skip to content

Commit 7778473

Browse files
authored
Update 2033-minimum-operations-to-make-a-uni-value-grid.js
1 parent 78d53fc commit 7778473

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
@@ -47,3 +47,31 @@ const minOperations = function(grid, x) {
4747
// 2 4 6 8
4848
// 1 2 3 5
4949

50+
// another
51+
52+
/**
53+
* @param {number[][]} grid
54+
* @param {number} x
55+
* @return {number}
56+
*/
57+
const minOperations = function(grid, x) {
58+
const arr = [], m = grid.length, n = grid[0].length
59+
for(let i = 0; i < m; i++) {
60+
for(let j = 0; j < n; j++) {
61+
arr.push(grid[i][j])
62+
}
63+
}
64+
arr.sort((a, b) => a - b)
65+
const mid = arr[~~((m * n) / 2)]
66+
let res = 0
67+
68+
for(let e of arr) {
69+
if (e !== mid) {
70+
const cur = Math.abs(e - mid)
71+
if(cur % x !== 0) return -1
72+
res += cur / x
73+
}
74+
}
75+
return res
76+
};
77+

0 commit comments

Comments
 (0)