🚀 LeetCode Daily Task: Minimum Operations to Make a Uni-Value Grid (2033) #23
-
🚀 LeetCode Daily Task: Minimum Operations to Make a Uni-Value Grid (2033)Difficulty: Medium 📝 Problem StatementYou are given a 2D integer grid of size A uni-value grid is a grid where all elements are equal. Your task: Find the minimum number of operations required to make the grid uni-value. If it is not possible, return 💡 Example 1:
💡 Example 2:
💡 Example 3:
🛠 Constraints:
🎯 How to Contribute [Click Here]✅ Step 1: Fork this repository. git clone https://github.com/YOUR-USERNAME/Optimism-Educator.git ✅ Step 3: Create a new branch. git checkout -b feature-leetcode2033 ✅ Step 4: Solve the problem in Python, Java, or C++. 📂 File Structure
🔥 Want to Participate?🌟 Star the repo & Join the Discussion [HERE] 🚀 🔗 Useful Links📌 LeetCode Problem Link: [Click Here] 💡 Let's learn, code, and grow together! 🚀 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
✅ Intuition and ApproachTo make a grid uni-value, all values must be equal. We can add or subtract 🧠 Key Observations:
🪜 Steps:
🧪 ExamplesExample 1:
Example 3:
✅ Python Codedef min_operations(grid, x):
# Flatten the grid
flat = [num for row in grid for num in row]
# Modulo check: all values should have same remainder mod x
remainder = flat[0] % x
if any(val % x != remainder for val in flat):
return -1
# Sort to find the median
flat.sort()
median = flat[len(flat) // 2]
# Compute operations
return sum(abs(val - median) // x for val in flat) 🛠 Constraints:
|
Beta Was this translation helpful? Give feedback.
✅ 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:
x
for it to be possible.x
is the same.🪜 Steps:
x
are the same.For each value:
abs(value - median) // x
🧪 Examples
Example 1:
grid = [[2, 4], [6, 8]], x = 2
Flattened:
[2…