The Reconstruct a 2-Row Binary Matrix problem gives you upper and lower row sums and a list of column sums. Find a 2×n binary matrix where row 1 has upper ones, row 2 has lower ones, and column j has colsum[j] ones. This coding problem uses greedy assignment based on column sum values. The array, matrix, and greedy interview pattern is demonstrated.
American Express and Grab ask this to test greedy construction: columns with colsum=2 must have both rows = 1; columns with colsum=1 get one 1 (assigned greedily to whichever row still needs more ones). If constraints conflict, return empty.
Greedy column-wise assignment. First, assign 1s to both rows for all columns with colsum=2 (mandatory). Check if upper/lower have sufficient remaining capacity. For columns with colsum=1: if upper > 0, assign to row 1; else assign to row 2. After processing, verify all row sums are exactly 0 (all ones assigned).
upper=2, lower=1, colsum=[1,1,1].
Binary matrix construction problems are solved greedily by processing most-constrained columns first. Colsum=2 forces both rows to 1; colsum=1 gives flexibility. After mandatory assignments, distribute the flexible ones to whichever row needs more. Validate at the end. Practice similar "fill matrix under row/column sum constraints" problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Valid Matrix Given Row and Column Sums | Medium | Solve | |
| Max Increase to Keep City Skyline | Medium | Solve | |
| Maximum Matrix Sum | Medium | Solve | |
| Minimum Operations to Make Columns Strictly Increasing | Easy | Solve | |
| Largest Submatrix With Rearrangements | Medium | Solve |