978. Minimize Maximum Value in Grid
An integer matrix grid of m x n unique positive numbers is presented to you.
Every integer in the matrix must be changed to a positive integer that satisfies the following requirements:
- Following the replacements, the relative order of each pair of elements in the same row or column should remain unchanged.
- Following the substitutions, the maximum number in the matrix should be as low as feasible.Â
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 = [ [1, 2, 3], [4, 5, 6] ]
Output : [ [1, 2, 3], [2, 3, 4] ]
Explanation : The below diagram shows a valid replacement.

The maximum number in the matrix is 4. It can be shown that no smaller value can be obtained.
Example 2:
Input : grid = [ [10, 12] ]
Output : [ [1, 2] ]
Explanation :
The below diagram shows a valid replacement.
The maximum number in the matrix is 4. It can be shown that no smaller value can be obtained.
Now Your Turn!
Pick the correct output for the given inputInput : grid = [ [6, 2, 9], [1, 5, 3], [7, 4, 8] ]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 1000
- 1 <= m * n <= 105
- 1 <= grid[i][j] <= 109
- grid consists of distinct integers.