Number of Laser Beams in a Bank - Problem
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix.
bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while '1' means the cell has a security device.
There is one laser beam between any two security devices if both conditions are met:
- The two devices are located on two different rows:
r1andr2, wherer1 < r2. - For each row
iwherer1 < i < r2, there are no security devices in theithrow.
Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Input & Output
Example 1 — Basic Bank Layout
$
Input:
bank = ["011001","000000","010100","001000"]
›
Output:
8
💡 Note:
Row 0 has 3 devices, Row 2 has 2 devices, Row 3 has 1 device. Beams: 3×2=6 (Row 0 to Row 2), 2×1=2 (Row 2 to Row 3). Total: 6+2=8
Example 2 — Single Row
$
Input:
bank = ["000","111","000"]
›
Output:
0
💡 Note:
Only one row has devices. No laser beams possible since beams require two different rows with devices.
Example 3 — Adjacent Active Rows
$
Input:
bank = ["11","11"]
›
Output:
4
💡 Note:
Row 0 has 2 devices, Row 1 has 2 devices. Total beams: 2×2=4 between the adjacent rows.
Constraints
- m == bank.length
- n == bank[i].length
- 1 ≤ m, n ≤ 500
- bank[i][j] is either '0' or '1'.
Visualization
Tap to expand
Understanding the Visualization
1
Input Bank
2D grid with '1' for devices, '0' for empty cells
2
Find Connections
Devices connect only across consecutive non-empty rows
3
Count Beams
Multiply device counts between adjacent active rows
Key Takeaway
🎯 Key Insight: Laser beams connect all devices between consecutive non-empty rows - multiply device counts for total beams
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code