Leetcode Problem 2033. Minimum Operations to Make a Uni-Value Grid

2033. Minimum Operations to Make a Uni-Value Grid

Leetcode Solutions

Prove the median answer

  1. Flatten the 2D grid into a 1D array and sort it.
  2. Find the median of the sorted array.
  3. Iterate over the array and calculate the difference between each element and the median.
  4. If the difference is not divisible by x, return -1 as it's impossible to make the grid uni-value.
  5. Otherwise, add the quotient of the difference divided by x to a count variable.
  6. Return the count variable as the minimum number of operations required.
UML Thumbnail

Two swipes vs. Median

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...