Skip to content

🚀 LeetCode Daily Task: Minimum Operations to Make a Uni-Value Grid (2033) #23

Answered by iamAntimPal
Antim-IWP asked this question in Q&A
Discussion options

You must be logged in to vote

Intuition and Approach

To make a grid uni-value, all values must be equal. We can add or subtract x from any element, any number of times.

🧠 Key Observations:

  • The difference between any two values in the grid must be divisible by x for it to be possible.
  • So we must check if every grid value modulo x is the same.
  • If possible, the optimal target value (to minimize operations) is the median of the values.

🪜 Steps:

  1. Flatten the grid into a list.
  2. Check if all values modulo x are the same.
  3. Sort the list.
  4. Choose the median as the target.
  5. Calculate total operations:
    For each value: abs(value - median) // x
  6. Return the total.

🧪 Examples

Example 1:

grid = [[2, 4], [6, 8]], x = 2
Flattened: [2…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by iamAntimPal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants