Reconstruct a 2-Row Binary Matrix - Problem

You're tasked with reconstructing a mysterious 2-row binary matrix from scattered clues! ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Given a matrix with n columns and exactly 2 rows, you know:

  • ๐Ÿ“Š Each cell contains only 0 or 1 (binary matrix)
  • ๐Ÿ”ข The sum of the upper row equals upper
  • ๐Ÿ”ข The sum of the lower row equals lower
  • ๐Ÿ“‹ An array colsum[i] telling you the sum of each column i

Your mission: Reconstruct the original 2ร—n matrix! If multiple valid solutions exist, return any one. If no solution is possible, return an empty array [].

Example: If upper=2, lower=1, colsum=[1,1,1], one valid matrix could be:
[[1,1,0], [0,0,1]] โœ…

Input & Output

example_1.py โ€” Basic Case
$ Input: upper = 2, lower = 1, colsum = [1,1,1]
โ€บ Output: [[1,1,0],[0,0,1]]
๐Ÿ’ก Note: Upper row sum: 1+1+0=2 โœ“, Lower row sum: 0+0+1=1 โœ“, Column sums: [1,1,1] โœ“
example_2.py โ€” Impossible Case
$ Input: upper = 1, lower = 1, colsum = [2,1,1]
โ€บ Output: []
๐Ÿ’ก Note: Column 0 needs sum=2 (both cells=1), but this would use up both row quotas, leaving no 1s for columns 1 and 2
example_3.py โ€” All Zeros
$ Input: upper = 0, lower = 0, colsum = [0,0,0]
โ€บ Output: [[0,0,0],[0,0,0]]
๐Ÿ’ก Note: All sums are 0, so the entire matrix is filled with zeros

Constraints

  • 1 โ‰ค colsum.length โ‰ค 105
  • 0 โ‰ค upper, lower โ‰ค colsum.length
  • 0 โ‰ค colsum[i] โ‰ค 2 (each column can have at most 2 ones in a 2-row matrix)

Visualization

Tap to expand
๐ŸŽญ Theatre Seating ReconstructionGiven: upper=2, lower=1, colsum=[1,1,1]Column 1Column 2Column 3๐Ÿ‘ค๐Ÿ’บSum: 1 โœ“๐Ÿ‘ค๐Ÿ’บSum: 1 โœ“๐Ÿ’บ๐Ÿ‘คSum: 1 โœ“Upper (2)Lower (1)๐Ÿ” Greedy Algorithm StepsStep 1: Process Column 1 (sum=1)โ€ข upper=2 > 0, so assign seat to upper rowโ€ข upper becomes 1, lower stays 1Step 2: Process Column 2 (sum=1)โ€ข upper=1 > 0, so assign seat to upper rowโ€ข upper becomes 0, lower stays 1Step 3: Process Column 3 (sum=1)โ€ข upper=0, lower=1 > 0, assign to lower rowโ€ข Both quotas now 0 โ†’ Valid solution! โœ…๐ŸŽฏ Key Insights๐Ÿ’ก Greedy Choice:Always prioritize upper rowwhen quotas are availableโšก Efficiency:O(n) time, O(1) extra spaceSingle pass solution๐Ÿšซ Handles Impossible:Returns [] when no validarrangement existsโœ… Natural Validation:Quota tracking ensuresconstraint satisfaction
Understanding the Visualization
1
Analyze Constraints
You have row quotas (upper=2, lower=1) and column requirements (colsum=[1,1,1])
2
Greedy Assignment
For each column, if sum=2 both seats occupied; if sum=1 assign to row with remaining quota; if sum=0 both empty
3
Verify Solution
Check that all row quotas are exactly fulfilled - no over/under assignment
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because each column's constraint immediately determines how to fill it - we just need to track remaining row quotas and assign greedily to ensure all constraints are satisfied.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.7K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen