2125. Number of Laser Beams in a Bank Medium

@problem@discussion
#Array#Math#String#Matrix



1/**
2 * [2125] Number of Laser Beams in a Bank
3 *
4 * 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 i^th row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.
5 * There is one laser beam between any two security devices if both conditions are met:
6 * 
7 * 	The two devices are located on two different rows: r1 and r2, where r1 < r2.
8 * 	For each row i where r1 < i < r2, there are no security devices in the i^th row.
9 * 
10 * Laser beams are independent, i.e., one beam does not interfere nor join with another.
11 * Return the total number of laser beams in the bank.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg" style="width: 400px; height: 368px;" />
15 * Input: bank = ["011001","000000","010100","001000"]
16 * Output: 8
17 * Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
18 *  * bank[0][1] -- bank[2][1]
19 *  * bank[0][1] -- bank[2][3]
20 *  * bank[0][2] -- bank[2][1]
21 *  * bank[0][2] -- bank[2][3]
22 *  * bank[0][5] -- bank[2][1]
23 *  * bank[0][5] -- bank[2][3]
24 *  * bank[2][1] -- bank[3][2]
25 *  * bank[2][3] -- bank[3][2]
26 * Note that there is no beam between any device on the 0^th row with any on the 3^rd row.
27 * This is because the 2^nd row contains security devices, which breaks the second condition.
28 * 
29 * Example 2:
30 * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg" style="width: 244px; height: 325px;" />
31 * Input: bank = ["000","111","000"]
32 * Output: 0
33 * Explanation: There does not exist two devices located on two different rows.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	m == bank.length
39 * 	n == bank[i].length
40 * 	1 <= m, n <= 500
41 * 	bank[i][j] is either '0' or '1'.
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/number-of-laser-beams-in-a-bank/
47// discuss: https://leetcode.com/problems/number-of-laser-beams-in-a-bank/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn number_of_beams(bank: Vec<String>) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_2125() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.