Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make a Uni-Value Grid

med
30 min
Try to solve the Minimum Operations to Make a Uni-Value Grid problem.

Statement

You are given a 22D integer grid grid of size m $ imes$ n, and an integer x.

In one operation, you can choose any element in grid and either add x to it or subtract x from it.

A grid is considered uni value if all of its elements are equal.

Return the minimum number of operations needed to make grid uni value. If it is impossible to make all elements equal, return −1-1.

Constraints:

  • m ==== grid.length

  • n ==== grid[i].length

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 1≤1 \leq m $ imes$ n ≤105\leq 10^5

  • 1≤1 \leq x, grid[i][j] ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make a Uni-Value Grid

med
30 min
Try to solve the Minimum Operations to Make a Uni-Value Grid problem.

Statement

You are given a 22D integer grid grid of size m $ imes$ n, and an integer x.

In one operation, you can choose any element in grid and either add x to it or subtract x from it.

A grid is considered uni value if all of its elements are equal.

Return the minimum number of operations needed to make grid uni value. If it is impossible to make all elements equal, return −1-1.

Constraints:

  • m ==== grid.length

  • n ==== grid[i].length

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 1≤1 \leq m $ imes$ n ≤105\leq 10^5

  • 1≤1 \leq x, grid[i][j] ≤104\leq 10^4