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:

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