Skip to content

Practice Reconstruct a 2-Row Binary Matrix

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1253
LeetCode Problem

Reconstruct a 2-Row Binary Matrix

Problem Statement

Construct a matrix with two rows and colsum.length columns containing only 0 or 1. Its top-row sum must equal upper, its bottom-row sum must equal lower, and column i must sum to colsum[i]. Return any matrix meeting all three requirements, or an empty two-dimensional array if none exists.

Source: Reconstruct a 2-Row Binary Matrix on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
upper = 2, lower = 1, colsum = [1,1,1]
Output
[[1,1,0],[0,0,1]]

# 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]]

Constraints

1 <= colsum.length <= 10^5
0 <= upper, lower <= colsum.length
0 <= colsum[i] <= 2

Related LeetCode Problems

Practice more problems similar to Reconstruct a 2-Row Binary Matrix.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.