Class Solution
- java.lang.Object
-
- g2001_2100.s2033_minimum_operations_to_make_a_uni_value_grid.Solution
-
public class Solution extends Object
2033 - Minimum Operations to Make a Uni-Value Grid.Medium
You are given a 2D integer
gridof sizem x nand an integerx. In one operation, you can addxto or subtractxfrom any element in thegrid.A uni-value grid is a grid where all the elements of it are equal.
Return the minimum number of operations to make the grid uni-value. If it is not possible, return
-1.Example 1:

Input: grid = [[2,4],[6,8]], x = 2
Output: 4
Explanation: We can make every element equal to 4 by doing the following:
-
Add x to 2 once.
-
Subtract x from 6 once.
-
Subtract x from 8 twice.
A total of 4 operations were used.
Example 2:

Input: grid = [[1,5],[2,3]], x = 1
Output: 5
Explanation: We can make every element equal to 3.
Example 3:

Input: grid = [[1,2],[3,4]], x = 2
Output: -1
Explanation: It is impossible to make every element equal.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 1051 <= m * n <= 1051 <= x, grid[i][j] <= 104
-
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intminOperations(int[][] grid, int x)
-