Last active
October 27, 2025 17:11
-
-
Save tatsuyax25/48257e85e8bb6ecc2930dcd5ced31657 to your computer and use it in GitHub Desktop.
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 t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string[]} bank | |
| * @return {number} | |
| */ | |
| // Function to calculate the total number of laser beams between security devices | |
| var numberOfBeams = function(bank) { | |
| // Array to store the count of devices (i.e., '1's) in each non-empty row | |
| let arr = []; | |
| // Variable to accumulate the total number of beams | |
| let result = 0; | |
| // Iterate through each row in the bank | |
| for (let i = 0; i < bank.length; i++) { | |
| // Count the number of '1's (devices) in the current row | |
| let total = 0; | |
| for (let j = 0; j < bank[i].length; j++) { | |
| // Convert character to number and check if it's a device ('1') | |
| if (Number(bank[i][j]) === 1) { | |
| total += 1; | |
| } | |
| } | |
| // Only consider rows that contain at least one device | |
| if (total > 0) { | |
| arr.push(total); | |
| } | |
| } | |
| // Calculate beams between each pair of consecutive non-empty rows | |
| // The number of beams between two rows is the product of their device counts | |
| for (let i = 0; i < arr.length - 1; i++) { | |
| result += arr[i] * arr[i + 1]; | |
| } | |
| // Return the total number of beams | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment