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 of every two elements that are in the same row or column should stay the same after the replacements.
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 theresulting matrix. If there are multiple answers, return
any of them.

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 is2. It can be shown that no smaller value can be obtained.
Example 2:
1
2
3
Input: grid =[[10]]Output: [[1]]Explanation: We replace the only number in the matrix with1.
To minimize the maximum value after replacements while preserving the relative order in each row and column, treat each cell as a node in a directed graph. Add edges for each row and column according to the original order. Use topological sorting to assign the smallest possible values to each cell, ensuring all constraints are satisfied.
classSolution {
public:int minimizeMaxValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> ans(m, vector<int>(n));
vector<vector<int>> indeg(m, vector<int>(n));
vector<vector<vector<pair<int,int>>>> g(m, vector<vector<pair<int,int>>>(n));
// Build graph for rows
for (int i =0; i < m; ++i) {
vector<pair<int,int>> row;
for (int j =0; j < n; ++j) row.push_back({grid[i][j], j});
sort(row.begin(), row.end());
for (int k =1; k < n; ++k) {
g[i][row[k-1].second].push_back({i, row[k].second});
indeg[i][row[k].second]++;
}
}
// Build graph for columns
for (int j =0; j < n; ++j) {
vector<pair<int,int>> col;
for (int i =0; i < m; ++i) col.push_back({grid[i][j], i});
sort(col.begin(), col.end());
for (int k =1; k < m; ++k) {
g[col[k-1].second][j].push_back({col[k].second, j});
indeg[col[k].second][j]++;
}
}
queue<pair<int,int>> q;
for (int i =0; i < m; ++i) for (int j =0; j < n; ++j) if (indeg[i][j] ==0) q.push({i,j});
int val =1;
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
ans[x][y] = val++;
for (auto& [nx, ny] : g[x][y]) {
if (--indeg[nx][ny] ==0) q.push({nx, ny});
}
}
int mx =0;
for (int i =0; i < m; ++i) for (int j =0; j < n; ++j) mx = max(mx, ans[i][j]);
return mx;
}
};
classSolution {
publicintminimizeMaxValue(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] ans =newint[m][n];
int[][] indeg =newint[m][n];
List<int[]>[][] g =new ArrayList[m][n];
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) g[i][j]=new ArrayList<>();
for (int i = 0; i < m; i++) {
int[][] row =newint[n][2];
for (int j = 0; j < n; j++) row[j]=newint[]{grid[i][j], j};
Arrays.sort(row, Comparator.comparingInt(a -> a[0]));
for (int k = 1; k < n; k++) {
g[i][row[k-1][1]].add(newint[]{i, row[k][1]});
indeg[i][row[k][1]]++;
}
}
for (int j = 0; j < n; j++) {
int[][] col =newint[m][2];
for (int i = 0; i < m; i++) col[i]=newint[]{grid[i][j], i};
Arrays.sort(col, Comparator.comparingInt(a -> a[0]));
for (int k = 1; k < m; k++) {
g[col[k-1][1]][j].add(newint[]{col[k][1], j});
indeg[col[k][1]][j]++;
}
}
Queue<int[]> q =new LinkedList<>();
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (indeg[i][j]== 0) q.offer(newint[]{i, j});
int val = 1;
while (!q.isEmpty()) {
int[] cur = q.poll();
int x = cur[0], y = cur[1];
ans[x][y]= val++;
for (int[] nb : g[x][y]) {
int nx = nb[0], ny = nb[1];
if (--indeg[nx][ny]== 0) q.offer(newint[]{nx, ny});
}
}
int mx = 0;
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) mx = Math.max(mx, ans[i][j]);
return mx;
}
}
classSolution {
funminimizeMaxValue(grid: Array<IntArray>): Int {
val m = grid.size; val n = grid[0].size
val ans = Array(m) { IntArray(n) }
val indeg = Array(m) { IntArray(n) }
val g = Array(m) { Array(n) { mutableListOf<Pair<Int,Int>>() } }
for (i in0 until m) {
val row = Array(n) { j -> grid[i][j] to j }
row.sortBy { it.first }
for (k in1 until n) {
g[i][row[k-1].second].add(i to row[k].second)
indeg[i][row[k].second]++ }
}
for (j in0 until n) {
val col = Array(m) { i -> grid[i][j] to i }
col.sortBy { it.first }
for (k in1 until m) {
g[col[k-1].second][j].add(col[k].second to j)
indeg[col[k].second][j]++ }
}
val q = ArrayDeque<Pair<Int,Int>>()
for (i in0 until m) for (j in0 until n) if (indeg[i][j] ==0) q.add(i to j)
var valNum = 1while (q.isNotEmpty()) {
val(x, y) = q.removeFirst()
ans[x][y] = valNum++for ((nx, ny) in g[x][y]) {
indeg[nx][ny]--if (indeg[nx][ny] ==0) q.add(nx to ny)
}
}
var mx = 0for (i in0 until m) for (j in0 until n) mx = maxOf(mx, ans[i][j])
return mx
}
}
defminimize_max_value(grid: list[list[int]]) -> int:
from collections import deque
m, n = len(grid), len(grid[0])
ans = [[0]*n for _ in range(m)]
indeg = [[0]*n for _ in range(m)]
g = [[[] for _ in range(n)] for _ in range(m)]
for i in range(m):
row = sorted([(grid[i][j], j) for j in range(n)])
for k in range(1, n):
g[i][row[k-1][1]].append((i, row[k][1]))
indeg[i][row[k][1]] +=1for j in range(n):
col = sorted([(grid[i][j], i) for i in range(m)])
for k in range(1, m):
g[col[k-1][1]][j].append((col[k][1], j))
indeg[col[k][1]][j] +=1 q = deque()
for i in range(m):
for j in range(n):
if indeg[i][j] ==0:
q.append((i, j))
val =1while q:
x, y = q.popleft()
ans[x][y] = val
val +=1for nx, ny in g[x][y]:
indeg[nx][ny] -=1if indeg[nx][ny] ==0:
q.append((nx, ny))
mx =0for i in range(m):
for j in range(n):
mx = max(mx, ans[i][j])
return mx