#1253
Reconstruct a 2-Row Binary Matrix
specialist · 895 · lc medium +31 · verified · 48.7% accepted · 484 likes · top 35%
Description
Reconstruct a 2-row binary matrix with n columns given these constraints:
- Each element is 0 or 1.
- The sum of row 0 (upper) equals upper.
- The sum of row 1 (lower) equals lower.
- The sum of column i equals colsum[i].
Return any valid matrix satisfying all constraints, or an empty 2D array if no solution exists.
Example 1:
Input: upper = 2, lower = 1, colsum = [1,1,1]
Output: [[1,1,0],[0,0,1]]
Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
Example 2:
Input: upper = 2, lower = 3, colsum = [2,2,1,1]
Output: []
Example 3:
Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
Code
1
2
3