Minimize Maximum Value in a Grid - Problem

You are given an m x n integer matrix grid containing distinct positive integers. Your task is to replace each integer in the matrix with a positive integer while satisfying these crucial conditions:

  1. Preserve relative order: The relative order of every two elements that are in the same row or column must stay the same after replacements
  2. Minimize maximum: The maximum number in the matrix after replacements should be as small as possible

The relative order condition means: if grid[r1][c1] > grid[r2][c2] where either r1 == r2 or c1 == c2, then this inequality must remain true after replacements.

Example: For matrix [[2, 4, 5], [7, 3, 9]], valid replacements include [[1, 2, 3], [2, 1, 4]] or [[1, 2, 3], [3, 1, 4]].

Return the resulting matrix. If multiple valid answers exist, return any of them.

Input & Output

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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