|
| 1 | +# [2371. Minimize Maximum Value in a Grid](https://leetcode.cn/problems/minimize-maximum-value-in-a-grid) |
| 2 | + |
| 3 | +[English Version](/solution/2300-2399/2371.Minimize%20Maximum%20Value%20in%20a%20Grid/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given an <code>m x n</code> integer matrix <code>grid</code> containing <strong>distinct</strong> positive integers.</p> |
| 10 | + |
| 11 | +<p>You have to replace each integer in the matrix with a positive integer satisfying the following conditions:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>The <strong>relative</strong> order of every two elements that are in the same row or column should stay the <strong>same</strong> after the replacements.</li> |
| 15 | + <li>The <strong>maximum</strong> number in the matrix after the replacements should be as <strong>small</strong> as possible.</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p>The relative order stays the same if for all pairs of elements in the original matrix such that <code>grid[r<sub>1</sub>][c<sub>1</sub>] > grid[r<sub>2</sub>][c<sub>2</sub>]</code> where either <code>r<sub>1</sub> == r<sub>2</sub></code> or <code>c<sub>1</sub> == c<sub>2</sub></code>, then it must be true that <code>grid[r<sub>1</sub>][c<sub>1</sub>] > grid[r<sub>2</sub>][c<sub>2</sub>]</code> after the replacements.</p> |
| 19 | + |
| 20 | +<p>For example, if <code>grid = [[2, 4, 5], [7, 3, 9]]</code> then a good replacement could be either <code>grid = [[1, 2, 3], [2, 1, 4]]</code> or <code>grid = [[1, 2, 3], [3, 1, 4]]</code>.</p> |
| 21 | + |
| 22 | +<p>Return <em>the <strong>resulting</strong> matrix.</em> If there are multiple answers, return <strong>any</strong> of them.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Example 1:</strong></p> |
| 26 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/08/09/grid2drawio.png" style="width: 371px; height: 121px;" /> |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> grid = [[3,1],[2,5]] |
| 29 | +<strong>Output:</strong> [[2,1],[1,2]] |
| 30 | +<strong>Explanation:</strong> The above diagram shows a valid replacement. |
| 31 | +The maximum number in the matrix is 2. It can be shown that no smaller value can be obtained. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong>Example 2:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> grid = [[10]] |
| 38 | +<strong>Output:</strong> [[1]] |
| 39 | +<strong>Explanation:</strong> We replace the only number in the matrix with 1. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>m == grid.length</code></li> |
| 47 | + <li><code>n == grid[i].length</code></li> |
| 48 | + <li><code>1 <= m, n <= 1000</code></li> |
| 49 | + <li><code>1 <= m * n <= 10<sup>5</sup></code></li> |
| 50 | + <li><code>1 <= grid[i][j] <= 10<sup>9</sup></code></li> |
| 51 | + <li><code>grid</code> consists of distinct integers.</li> |
| 52 | +</ul> |
| 53 | + |
| 54 | + |
| 55 | +## 解法 |
| 56 | + |
| 57 | +<!-- 这里可写通用的实现逻辑 --> |
| 58 | + |
| 59 | +<!-- tabs:start --> |
| 60 | + |
| 61 | +### **Python3** |
| 62 | + |
| 63 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 64 | + |
| 65 | +```python |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +### **Java** |
| 70 | + |
| 71 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 72 | + |
| 73 | +```java |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +### **TypeScript** |
| 78 | + |
| 79 | +```ts |
| 80 | + |
| 81 | +``` |
| 82 | + |
| 83 | +### **...** |
| 84 | + |
| 85 | +``` |
| 86 | +
|
| 87 | +``` |
| 88 | + |
| 89 | +<!-- tabs:end --> |
0 commit comments