Watch 5 video solutions for Minimize Maximum Value in a Grid, a hard level problem involving Array, Union Find, Graph. This walkthrough by Code-Yao has 655 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
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.
Example 1:
Input: grid = [[3,1],[2,5]] Output: [[2,1],[1,2]] Explanation: The above diagram shows a valid replacement. The maximum number in the matrix is 2. It can be shown that no smaller value can be obtained.
Example 2:
Input: grid = [[10]] Output: [[1]] Explanation: We replace the only number in the matrix with 1.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 10001 <= m * n <= 1051 <= grid[i][j] <= 109grid consists of distinct integers.Problem Overview: You receive an m x n matrix. You must replace each cell with a positive integer so that ordering constraints in every row and column are preserved. If grid[i][j] < grid[x][y] in the same row or column, the assigned value must also be smaller. The objective is to minimize the maximum assigned value across the grid.
Approach 1: Row/Column Simulation (Brute Force) (Time: O((mn)^2), Space: O(mn))
A direct idea is to repeatedly compare every pair of cells that share a row or column and enforce ordering constraints. For each cell, compute the largest value required based on smaller elements in its row and column. This often requires reprocessing cells whenever a constraint changes, leading to quadratic behavior over all mn cells. The method models the dependency rules correctly but becomes impractical for large grids.
Approach 2: Graph Modeling + Topological Sort (Time: O(mn log(mn)), Space: O(mn))
Treat each cell as a node in a directed graph. If two cells share a row or column and one value is smaller, create a directed edge from the smaller to the larger node. After building the graph, perform topological sort and assign ranks in order of dependencies. The rank of a node becomes max(parent ranks) + 1. Sorting cells by value helps build edges efficiently. This models the constraint system explicitly but constructing all edges can still be expensive in dense rows and columns.
Approach 3: Sorting + Union Find on Rows and Columns (Optimal) (Time: O(mn log(mn)), Space: O(m+n))
Process cells in ascending order of their original values using sorting. Cells with the same value must be processed together because they should receive the same rank relative to existing constraints. For each value group, connect its row and column indices using Union Find. Each connected component represents cells that must share the same rank level. Compute the rank for a component as max(rowRank[r], colRank[c]) + 1 across its members. After assigning ranks, update the row and column maximum ranks. This avoids building a full dependency graph and keeps operations near linear after sorting.
Recommended for interviews: The Union Find + sorting approach is what interviewers typically expect. The brute force explanation demonstrates you understand the ordering constraints, but the optimized method shows you can compress dependencies and avoid building a full graph. It scales cleanly to large matrices and combines sorting, union-find, and rank propagation in a concise implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Row/Column Simulation (Brute Force) | O((mn)^2) | O(mn) | Useful for understanding ordering constraints and validating logic on small grids |
| Graph + Topological Sort | O(mn log(mn)) | O(mn) | When modeling dependencies explicitly as a DAG between cells |
| Sorting + Union Find (Optimal) | O(mn log(mn)) | O(m+n) | Best general solution for large matrices; avoids building a full dependency graph |