> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Laser Beams in a Bank

> Tested Python solution for LeetCode 2125 with 32 pytest cases. Generate a practice environment with lcpy.

LeetCode 2125, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [String](/catalog/topics/string), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/).

Generate this problem as a practice environment: tested reference solution, 32 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2125   # by problem number
lcpy gen -s number_of_laser_beams_in_a_bank   # by problem name
```

## 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**: `r1` and `r2`, where `r1 < r2`.
* For **each** row `i` where `r1 < i < r2`, there are **no security devices** in the `ith` row.

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*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg)

```
Input: bank = ["011001","000000","010100","001000"]
Output: 8
```

**Explanation:** Between each of the following device pairs, there is one beam. In total, there are 8 beams:

* bank\[0]\[1] -- bank\[2]\[1]
* bank\[0]\[1] -- bank\[2]\[3]
* bank\[0]\[2] -- bank\[2]\[1]
* bank\[0]\[2] -- bank\[2]\[3]
* bank\[0]\[5] -- bank\[2]\[1]
* bank\[0]\[5] -- bank\[2]\[3]
* bank\[2]\[1] -- bank\[3]\[2]
* bank\[2]\[3] -- bank\[3]\[2]

Note that there is no beam between any device on the 0th row with any on the 3rd row. This is because the 2nd row contains security devices, which breaks the second condition.

![Example 2](https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg)

```
Input: bank = ["000","111","000"]
Output: 0
```

**Explanation:** There does not exist two devices located on two different rows.

### Constraints

* m == bank.length
* n == bank\[i].length
* 1 \<= m, n \<= 500
* bank\[i]\[j] is either '0' or '1'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_laser_beams_in_a_bank/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_laser_beams_in_a_bank/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(1)
    def number_of_beams(self, bank: list[str]) -> int:
        total = 0
        prev = 0
        for row in bank:
            count = row.count("1")
            if count:
                total += prev * count
                prev = count
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.