Minimize Maximum Value in a Grid - Problem

You are given an m x n integer matrix grid containing distinct positive integers. You have to replace each integer in the matrix with a positive integer satisfying the following conditions:

1. The relative order of every two elements that are in the same row or column should stay the same after the replacements.

2. The maximum number in the matrix after the replacements should be as small as possible.

The relative order stays the same if for all pairs of elements in the original matrix such that grid[r1][c1] > grid[r2][c2] where either r1 == r2 or c1 == c2, then it must be true that grid[r1][c1] > grid[r2][c2] after the replacements.

For example, if grid = [[2, 4, 5], [7, 3, 9]] then a good replacement could be either grid = [[1, 2, 3], [2, 1, 4]] or grid = [[1, 2, 3], [3, 1, 4]].

Return the resulting matrix. If there are multiple answers, return any of them.

Input & Output

Example 1 — Basic Grid
$ Input: grid = [[2,4,5],[7,3,9]]
Output: [[1,2,3],[2,1,4]]
💡 Note: Process elements by value: 2→rank 1, 3→rank 1, 4→rank 2, 5→rank 3, 7→rank 2, 9→rank 4. Each rank respects row/column ordering constraints.
Example 2 — Single Row
$ Input: grid = [[1,3,2]]
Output: [[1,3,2]]
💡 Note: In a single row, elements must maintain their relative order: 1 < 2 < 3, so the minimum assignment is [1,3,2].
Example 3 — Single Column
$ Input: grid = [[5],[2],[8]]
Output: [[2],[1],[3]]
💡 Note: In a single column, we process 2→1, 5→2, 8→3 to maintain the constraint 2 < 5 < 8.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 1000
  • 1 ≤ grid[i][j] ≤ 109
  • All integers in grid are distinct

Visualization

Tap to expand
Minimize Maximum Value in a Grid INPUT Original Grid (m x n) 2 4 5 7 3 9 R0 R1 C0 C1 C2 Sorted by value: 2, 3, 4, 5, 7, 9 Constraints: - All values distinct - Preserve relative order - Minimize maximum value grid = [[2,4,5],[7,3,9]] ALGORITHM STEPS 1 Sort All Elements Sort cells by value Process smallest first 2 Union-Find Setup Track row/col max values Union cells sharing row/col 3 Coordinate Compress new_val = max(row_max, col_max) + 1 4 Assign Values Update row/col trackers Continue to next cell Processing Order: 2(0,0)-->1 3(1,1)-->1 4(0,1)-->2 5(0,2)-->3 7(1,0)-->2 9(1,2)-->4 FINAL RESULT Compressed Grid 1 2 3 2 1 4 Verification: Row 0: 1 < 2 < 3 [OK] Row 1: 2 > 1 < 4 [OK] Col 0: 1 < 2 [OK] Col 1: 2 > 1 [OK] Col 2: 3 < 4 [OK] Maximum Value = 4 [[1,2,3],[2,1,4]] Key Insight: By sorting elements and processing from smallest to largest, we assign the minimum possible value to each cell while respecting row/column ordering constraints. Union-Find helps track connected components where cells must maintain consistent relative ordering across shared rows/columns. TutorialsPoint - Minimize Maximum Value in a Grid | Coordinate Compression with Union-Find
Asked in
Google 15 Amazon 8 Microsoft 6
15.6K Views
Medium Frequency
~35 min Avg. Time
425 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen