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.<p>Medium</p> <p>You are given a 2D integer <code>grid</code> of size <code>m x n</code> and an integer <code>x</code>. In one operation, you can <strong>add</strong> <code>x</code> to or <strong>subtract</strong> <code>x</code> from any element in the <code>grid</code>.</p> <p>A <strong>uni-value grid</strong> is a grid where all the elements of it are equal.</p> <p>Return <em>the <strong>minimum</strong> number of operations to make the grid <strong>uni-value</strong></em>. If it is not possible, return <code>-1</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png" alt="" /></p> <p><strong>Input:</strong> grid = [[2,4],[6,8]], x = 2</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> We can make every element equal to 4 by doing the following:</p> <ul> <li> <p>Add x to 2 once.</p> </li> <li> <p>Subtract x from 6 once.</p> </li> <li> <p>Subtract x from 8 twice.</p> </li> </ul> <p>A total of 4 operations were used.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png" alt="" /></p> <p><strong>Input:</strong> grid = [[1,5],[2,3]], x = 1</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> We can make every element equal to 3.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png" alt="" /></p> <p><strong>Input:</strong> grid = [[1,2],[3,4]], x = 2</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It is impossible to make every element equal.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 10<sup>5</sup></code></li> <li><code>1 <= m * n <= 10<sup>5</sup></code></li> <li><code>1 <= x, grid[i][j] <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minOperations

      public int minOperations(int[][] grid, int x)