2392. Build a Matrix With Conditions
Practice Build a Matrix With Conditions for free on NudgeCode: Hard, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Build a k x k matrix that contains each number from 1 to k exactly once; every other cell is 0. The matrix must also satisfy:
- for each
[above, below]inrowConditions, the row ofaboveis strictly above the row ofbelow - for each
[left, right]incolConditions, the column ofleftis strictly left of the column ofright
Return any matrix that meets all conditions; the expected value is one of them. If no such matrix exists, return an empty matrix [].
Example
k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
-> [[0,0,1],[3,0,0],[0,2,0]]
Rows: 1 (row 0) and 3 (row 1) are both above 2 (row 2). Columns: 3 (column 0) is left of 2 (column 1), which is left of 1 (column 2). [[3,0,0],[0,0,1],[0,2,0]] is also accepted.
Constraints
- 2 <= k <= 400
- 1 <= rowConditions.length, colConditions.length <= 10^4
- each condition is a pair of different numbers from 1 to k