|
| 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 | + */ |
| 44 | +pub 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 | + |
| 51 | +impl Solution { |
| 52 | + pub fn number_of_beams(bank: Vec<String>) -> i32 { |
| 53 | + bank.iter() |
| 54 | + .map(|s| s.chars().filter(|&c| c == '1').count()) |
| 55 | + .filter(|&i| i > 0) |
| 56 | + .collect::<Vec<_>>() |
| 57 | + .windows(2) |
| 58 | + .map(|x| x[0] * x[1]) |
| 59 | + .sum::<usize>() as _ |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// submission codes end |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use super::*; |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_2125_example_1() { |
| 71 | + let bank = vec_string!["011001", "000000", "010100", "001000"]; |
| 72 | + |
| 73 | + let result = 8; |
| 74 | + |
| 75 | + assert_eq!(Solution::number_of_beams(bank), result); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_2125_example_2() { |
| 80 | + let bank = vec_string!["000", "111", "000"]; |
| 81 | + |
| 82 | + let result = 0; |
| 83 | + |
| 84 | + assert_eq!(Solution::number_of_beams(bank), result); |
| 85 | + } |
| 86 | +} |
0 commit comments