MediumNo. 2125
Number of Laser Beams in a Bank — LeetCode 2125 Python Solution
- Pattern
- Matrix and Grid
- Topics
- Array, Math, String, Matrix
- Reading time
- 2 min
- Source
- leetcode.com
The 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.
Example
- 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:
Python solution
class Solution:
def numberOfBeams(self, bank: List[str]) -> int:
ans = pre = 0
for row in bank:
if (cur := row.count("1")) > 0:
ans += pre * cur
pre = cur
return ansComplexity
- Time
- O(m × n)where m and n are the number of rows and columns, respectively
- Space · auxiliary
- O(1)
Pattern: Matrix and Grid
Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2125. Number of Laser Beams in a Bank is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.
The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0048 | Rotate Image | Medium |
| 0883 | Projection Area of 3D Shapes | Easy |
| 0892 | Surface Area of 3D Shapes | Easy |
| 2575 | Find the Divisibility Array of a String | Medium |
| 2614 | Prime In Diagonal | Easy |
| 2946 | Matrix Similarity After Cyclic Shifts | Easy |
Frequently asked questions
- How hard is LeetCode 2125. Number of Laser Beams in a Bank?
LeetCode 2125. Number of Laser Beams in a Bank is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2125. Number of Laser Beams in a Bank?
The Python solution on this page runs in O(m × n), where m and n are the number of rows and columns, respectively.
- What is the space complexity of LeetCode 2125. Number of Laser Beams in a Bank?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2125. Number of Laser Beams in a Bank cover?
LeetCode 2125. Number of Laser Beams in a Bank is tagged Array, Math, String and Matrix on LeetCode.