2
def minOperations(self, grid: List[List[int]], x: int) -> int:11
# transform grid to array, easier to operate18
# the median is arr[len(arr)//2] when len(arr) is odd19
# or may be arr[len(arr)//2] and arr[len(arr)//2-1] when len(arr) is even.20
cand1 = arr[len(arr) // 2]21
cand2 = arr[len(arr) // 2 - 1]24
self.get_num_operations_to_target(grid, cand1, x),25
self.get_num_operations_to_target(grid, cand2, x),28
def get_num_operations_to_target(self, grid, target, x):29
"""Get the total number of operations to transform all grid elements to the target value."""31
for i in range(len(grid)):32
for j in range(len(grid[0])):33
if abs(grid[i][j] - target) % x != 0:36
ans += abs(grid[i][j] - target) // x