|
| 1 | +<h2><a href="https://leetcode.com/problems/build-a-matrix-with-conditions/">2392. Build a Matrix With Conditions</a></h2><h3>Hard</h3><hr><div><p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li> |
| 5 | + <li>a 2D integer array <code>colConditions</code> of size <code>m</code> where <code>colConditions[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>The two arrays contain integers from <code>1</code> to <code>k</code>.</p> |
| 9 | + |
| 10 | +<p>You have to build a <code>k x k</code> matrix that contains each of the numbers from <code>1</code> to <code>k</code> <strong>exactly once</strong>. The remaining cells should have the value <code>0</code>.</p> |
| 11 | + |
| 12 | +<p>The matrix should also satisfy the following conditions:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li>The number <code>above<sub>i</sub></code> should appear in a <strong>row</strong> that is strictly <strong>above</strong> the row at which the number <code>below<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>n - 1</code>.</li> |
| 16 | + <li>The number <code>left<sub>i</sub></code> should appear in a <strong>column</strong> that is strictly <strong>left</strong> of the column at which the number <code>right<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>m - 1</code>.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p>Return <em><strong>any</strong> matrix that satisfies the conditions</em>. If no answer exists, return an empty matrix.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/07/06/gridosdrawio.png" style="width: 211px; height: 211px;"> |
| 24 | +<pre><strong>Input:</strong> k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]] |
| 25 | +<strong>Output:</strong> [[3,0,0],[0,0,1],[0,2,0]] |
| 26 | +<strong>Explanation:</strong> The diagram above shows a valid example of a matrix that satisfies all the conditions. |
| 27 | +The row conditions are the following: |
| 28 | +- Number 1 is in row <u>1</u>, and number 2 is in row <u>2</u>, so 1 is above 2 in the matrix. |
| 29 | +- Number 3 is in row <u>0</u>, and number 2 is in row <u>2</u>, so 3 is above 2 in the matrix. |
| 30 | +The column conditions are the following: |
| 31 | +- Number 2 is in column <u>1</u>, and number 1 is in column <u>2</u>, so 2 is left of 1 in the matrix. |
| 32 | +- Number 3 is in column <u>0</u>, and number 2 is in column <u>1</u>, so 3 is left of 2 in the matrix. |
| 33 | +Note that there may be multiple correct answers. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre><strong>Input:</strong> k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]] |
| 39 | +<strong>Output:</strong> [] |
| 40 | +<strong>Explanation:</strong> From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied. |
| 41 | +No matrix can satisfy all the conditions, so we return the empty matrix. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>2 <= k <= 400</code></li> |
| 49 | + <li><code>1 <= rowConditions.length, colConditions.length <= 10<sup>4</sup></code></li> |
| 50 | + <li><code>rowConditions[i].length == colConditions[i].length == 2</code></li> |
| 51 | + <li><code>1 <= above<sub>i</sub>, below<sub>i</sub>, left<sub>i</sub>, right<sub>i</sub> <= k</code></li> |
| 52 | + <li><code>above<sub>i</sub> != below<sub>i</sub></code></li> |
| 53 | + <li><code>left<sub>i</sub> != right<sub>i</sub></code></li> |
| 54 | +</ul> |
| 55 | +</div> |
0 commit comments