2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution

In this guide, you will get 2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make a Uni-Value Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Make a Uni-Value Grid solution in C++
  4. Minimum Operations to Make a Uni-Value Grid solution in Java
  5. Minimum Operations to Make a Uni-Value Grid solution in Python
  6. Additional Resources
2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution image

Problem Statement of Minimum Operations to Make a Uni-Value Grid

You are given a 2D integer grid of size m x n and an integer x. In one operation, you can add x to or subtract x from any element in the grid.
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.length
n == grid[i].length
1 <= m, n <= 105
1 <= m * n <= 105
1 <= x, grid[i][j] <= 104

See also  811. Subdomain Visit Count LeetCode Solution

Complexity Analysis

  • Time Complexity: O(mn) (C++), O(mn\log mn) (Java/Python)
  • Space Complexity: O(mn)

2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution in C++

class Solution {
 public:
  int minOperations(vector<vector<int>>& grid, int x) {
    vector<int> arr;
    for (const vector<int>& row : grid)
      arr.insert(arr.end(), row.begin(), row.end());
    if (ranges::any_of(arr, [&](int a) { return (a - arr[0]) % x; }))
      return -1;

    int ans = 0;

    nth_element(arr.begin(), arr.begin() + arr.size() / 2, arr.end());

    for (const int a : arr)
      ans += abs(a - arr[arr.size() / 2]) / x;

    return ans;
  }
};
/* code provided by PROGIEZ */

2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution in Java

class Solution {
  public int minOperations(int[][] grid, int x) {
    final int m = grid.length;
    final int n = grid[0].length;
    int[] arr = new int[m * n];
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        arr[i * n + j] = grid[i][j];
    if (Arrays.stream(arr).anyMatch(a -> (a - arr[0]) % x != 0))
      return -1;

    int ans = 0;

    Arrays.sort(arr);

    for (final int a : arr)
      ans += Math.abs(a - arr[arr.length / 2]) / x;

    return ans;
  }
}
// code provided by PROGIEZ

2033. Minimum Operations to Make a Uni-Value Grid LeetCode Solution in Python

class Solution:
  def minOperations(self, grid: list[list[int]], x: int) -> int:
    arr = sorted([a for row in grid for a in row])
    if any((a - arr[0]) % x for a in arr):
      return -1

    ans = 0

    for a in arr:
      ans += abs(a - arr[len(arr) // 2]) // x

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.