2033. Minimum Operations to Make a Uni-Value Grid

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2033

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)