2033. Minimum Operations to Make a Uni-Value Grid
On This Page
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
nums = []
mod = grid[0][0] % x
for row in grid:
for v in row:
if v % x != mod:
return -1
nums.append(v)
nums.sort()
mid = nums[len(nums) >> 1]
return sum(abs(v - mid) // x for v in nums)