返回介绍

solution / 2000-2099 / 2033.Minimum Operations to Make a Uni-Value Grid / README_EN

发布于 2024-06-17 01:03:11 字数 5372 浏览 0 评论 0 收藏 0

2033. Minimum Operations to Make a Uni-Value Grid

中文文档

Description

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

Solutions

Solution 1: Greedy

Firstly, to make the grid a single-value grid, the remainder of all elements of the grid with $x$ must be the same.

Therefore, we can first traverse the grid to check whether the remainder of all elements with $x$ is the same. If not, return $-1$. Otherwise, we put all elements into an array, sort the array, take the median, then traverse the array, calculate the difference between each element and the median, divide it by $x$, and add all the differences to get the answer.

The time complexity is $O((m \times n) \times \log (m \times n))$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.

class Solution:
  def minOperations(self, grid: List[List[int]], x: int) -> int:
    nums = []
    mod = grid[0][0] % x
    for row in grid:
      for v in row:
        if v % x != mod:
          return -1
        nums.append(v)
    nums.sort()
    mid = nums[len(nums) >> 1]
    return sum(abs(v - mid) // x for v in nums)
class Solution {
  public int minOperations(int[][] grid, int x) {
    int m = grid.length, n = grid[0].length;
    int[] nums = new int[m * n];
    int mod = grid[0][0] % x;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] % x != mod) {
          return -1;
        }
        nums[i * n + j] = grid[i][j];
      }
    }
    Arrays.sort(nums);
    int mid = nums[nums.length >> 1];
    int ans = 0;
    for (int v : nums) {
      ans += Math.abs(v - mid) / x;
    }
    return ans;
  }
}
class Solution {
public:
  int minOperations(vector<vector<int>>& grid, int x) {
    int m = grid.size(), n = grid[0].size();
    int mod = grid[0][0] % x;
    int nums[m * n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] % x != mod) {
          return -1;
        }
        nums[i * n + j] = grid[i][j];
      }
    }
    sort(nums, nums + m * n);
    int mid = nums[(m * n) >> 1];
    int ans = 0;
    for (int v : nums) {
      ans += abs(v - mid) / x;
    }
    return ans;
  }
};
func minOperations(grid [][]int, x int) int {
  mod := grid[0][0] % x
  nums := []int{}
  for _, row := range grid {
    for _, v := range row {
      if v%x != mod {
        return -1
      }
      nums = append(nums, v)
    }
  }
  sort.Ints(nums)
  mid := nums[len(nums)>>1]
  ans := 0
  for _, v := range nums {
    ans += abs(v-mid) / x
  }
  return ans
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文