Number of Laser Beams in a Bank (LeetCode 2125): Solution and Explanation
Given a grid of binary strings where '1' marks a security device, count the beams. A beam joins two devices on different rows when every row between them has no device.
Intuition
Draw the floor plan and the beams. A device only fires at devices in the nearest row below it that has any devices. Empty rows in between are invisible: beams pass straight through them.
Within that pair of rows the positions do not matter. Every device in the upper row links to every device in the lower row, so a rows with a devices and b devices produce a * b beams, like a complete bipartite graph.
A device row never connects past the next device row, because that row would sit between them and block the beam. So the answer is the sum of a * b over consecutive pairs of non-empty rows.
That means you only need one remembered number while scanning: how many devices the last non-empty row had.
Approach: Running product of consecutive device rows
- Set
total = 0andprev = 0. - For each row, count its '1' characters as
cur. - If
cur == 0, skip the row. - Otherwise add
prev * curtototaland setprev = cur. - Return
total.
Step-by-step example
bank = ["011001","000000","010100","001000"]
- Row 0 has 3 devices. It is the first device row, so prev = 3 and total stays 0.
- Row 1 has no devices. Skip it.
- Row 2 has 2 devices. Add 3 * 2 = 6, total = 6, prev = 2.
- Row 3 has 1 device. Add 2 * 1 = 2, total = 8, prev = 1.
- Return 8.
Code: Python, Java, C++
1class Solution:
2 def numberOfBeams(self, bank: List[str]) -> int:
3 total = 0
4 prev = 0
5 for row in bank:
6 cur = row.count('1')
7 if cur == 0:
8 continue
9 total += prev * cur
10 prev = cur
11 return total1class Solution {
2 public int numberOfBeams(String[] bank) {
3 int total = 0;
4 int prev = 0;
5 for (String row : bank) {
6 int cur = 0;
7 for (char ch : row.toCharArray()) if (ch == '1') cur++;
8 if (cur == 0) continue;
9 total += prev * cur;
10 prev = cur;
11 }
12 return total;
13 }
14}1class Solution {
2public:
3 int numberOfBeams(vector<string>& bank) {
4 int total = 0;
5 int prev = 0;
6 for (const string& row : bank) {
7 int cur = 0;
8 for (char ch : row) if (ch == '1') cur++;
9 if (cur == 0) continue;
10 total += prev * cur;
11 prev = cur;
12 }
13 return total;
14 }
15};Time and space complexity
O(m * n)
Every character of the grid is read once to count devices.
O(1)
Only the previous count and the running total are stored.
Other approaches
Collect counts, then multiply neighbours (time O(m * n), space O(m))
Build a list of the non-zero row counts first, then sum counts[i] * counts[i + 1]. Same result and easy to read, with O(m) extra memory for the list.
Pair every two devices (time O((m * n)^2 * m), space O(1))
For every pair of devices on different rows, check that all rows between them are empty. It follows the statement literally but is far too slow for a 500 x 500 grid.
Common mistakes
- Resetting prev to 0 on an empty row. Empty rows do not block beams, so prev must survive them.
- Multiplying every pair of non-empty rows instead of only consecutive ones. A device row in between blocks the beam.
- Thinking positions matter. Beams are not vertical; any device in the upper row reaches any device in the lower row.
Frequently asked questions
Why does the column of a device not matter?
The beam rule only talks about rows. Two devices on consecutive device rows are joined no matter which columns they are in, so only the counts per row matter.
Can the total overflow a 32-bit integer?
No. With at most 500 rows of 500 devices each, the largest possible total is about 250 * 250000 = 62.5 million, well under 2^31.
What if no row or only one row has devices?
Then prev * cur is never added with a non-zero prev, and the answer is 0.