Ctrl K

Two Rows of Lamps

Medium
A display board has two rows of n lamps each; every lamp is either on (1) or off (0). The picture itself is lost, and only the counters survived: - top is the number of lamps that are on in the top row; - bottom is the number of lamps that are on in the bottom row; - column_lit[j] is the number of lamps that are on in column j (0 to 2). Restore the board and return it as a list of two rows: the top row first, then the bottom one. If the counters contradict each other, return an empty list. The input is guaranteed to allow at most one matching board.
Example 1
top=3, bottom=1, column_lit=[1, 2, 0, 1] → [[1, 1, 0, 1], [0, 1, 0, 0]]

Column 1 is fully lit, which uses one lamp in each row. The bottom row has nothing left, so the single lamps in columns 0 and 3 belong to the top row.

Example 2
top=1, bottom=1, column_lit=[2, 1] → []

Three lamps are lit in total, but the rows account for only 1 + 1 = 2. No such board exists, so the answer is empty.

Example 3
top=0, bottom=2, column_lit=[0, 1, 1] → [[0, 0, 0], [0, 1, 1]]
Constraints
  • 1 ≤ len(column_lit) ≤ 10⁵
  • 0 ≤ top, bottom ≤ len(column_lit)
  • column_lit[j] ∈ {0, 1, 2}
  • At most one board matches the counters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRaise Towers, Keep the Skyline MediumBest Binary Rows After Flips MediumRows Under the Diagonal MediumA Table From Its Totals MediumBiggest Block After a Column Shuffle Medium
loading Python…
Input
top=3, bottom=1, column_lit=[1, 2, 0, 1]
Expected
[[1, 1, 0, 1], [0, 1, 0, 0]]

Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.