Ctrl K

Alarm Beams Between Rows

Medium
A warehouse floor plan is given as a list of equal-length strings floor_plan: floor_plan[i] is row i, where 1 marks a sensor and 0 marks an empty spot. Two sensors are linked by a beam if they sit in different rows and no row strictly between them contains any sensor. Each such pair produces its own beam. Count the beams.
Example 1
floor_plan=['1010', '0000', '0110', '1000'] → 6

The empty row 1 is skipped. Rows 0 and 2 give 2 · 2 = 4 beams, rows 2 and 3 give 2 · 1 = 2. That is 6 in total.

Example 2
floor_plan=['111', '000'] → 0

All sensors share one row, and beams only run between different rows.

Example 3
floor_plan=['1', '1', '1'] → 2
Constraints
  • 1 ≤ len(floor_plan) ≤ 500
  • 1 ≤ len(floor_plan[i]) ≤ 500, all rows have the same length
  • Rows consist only of the characters 0 and 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTurn a Square Clockwise MediumWord Traced on a Letter Grid MediumClosest Clock Readings MediumFour-Corner Rectangles MediumMagic 3×3 Blocks Medium
loading Python…
Input
floor_plan=['1010', '0000', '0110', '1000']
Expected
6

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