Skip to content

Commit 1a2f1f3

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

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
@@ -75,3 +75,31 @@ const minOperations = function(grid, x) {
7575
return res
7676
};
7777

78+
// another
79+
80+
/**
81+
* @param {number[][]} grid
82+
* @param {number} x
83+
* @return {number}
84+
*/
85+
function minOperations(grid, x) {
86+
const m = grid.length, n = grid[0].length, mn = m * n, arr = []
87+
for(let i = 0; i < m; i++) {
88+
for(let j = 0; j < n; j++) {
89+
arr.push(grid[i][j])
90+
}
91+
}
92+
arr.sort((a, b) => a - b)
93+
const mid = arr[~~(mn / 2)]
94+
let res = 0
95+
96+
for(let e of arr) {
97+
if(e !== mid) {
98+
const delta = Math.abs(e - mid)
99+
if(delta % x !== 0) return -1
100+
res += delta / x
101+
}
102+
}
103+
104+
return res
105+
};

0 commit comments

Comments
 (0)