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:
r1 and r2, where r1 < r2.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.
Example 1:
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:
Input: bank = ["000","111","000"] Output: 0 Explanation: There does not exist two devices located on two different rows.
Constraints:
m == bank.lengthn == bank[i].length1 <= m, n <= 500bank[i][j] is either '0' or '1'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to figure out how many laser beams there are between rows in a bank, based on some arrangement of security devices. The basic approach is to look at every single row and compare it to every other row to see if there's a valid laser beam connection.
Here's how the algorithm would work step-by-step:
def number_of_laser_beams_brute_force(bank):
total_laser_beams = 0
number_of_rows = len(bank)
for first_row_index in range(number_of_rows):
# Check each row to see if it has devices.
number_of_devices_first_row = bank[first_row_index].count('1')
if number_of_devices_first_row > 0:
for second_row_index in range(first_row_index + 1, number_of_rows):
# Check if the second row also has devices
number_of_devices_second_row = bank[second_row_index].count('1')
if number_of_devices_second_row > 0:
rows_are_separated_by_empty_rows = True
#Verify that the rows in between are all empty
for intermediate_row_index in range(first_row_index + 1, second_row_index):
if bank[intermediate_row_index].count('1') > 0:
rows_are_separated_by_empty_rows = False
break
if rows_are_separated_by_empty_rows:
#If the rows in between are empty, increment.
total_laser_beams += number_of_devices_first_row * number_of_devices_second_row
return total_laser_beamsThe key idea is to focus on the rows that actually contain devices and skip the empty ones. We want to count how many device combinations exist between each pair of non-empty rows. Multiplying the number of devices in each consecutive pair of rows gives us the total number of laser beams.
Here's how the algorithm would work step-by-step:
def number_of_laser_beams_in_a_bank(bank):
total_beams = 0
previous_devices = 0
for row in bank:
device_count = row.count('1')
# Skip rows without any devices.
if device_count == 0:
continue
# Calculate beams based on previous row
if previous_devices > 0:
total_beams += device_count * previous_devices
# Store current row's device count
previous_devices = device_count
return total_beams| Case | How to Handle |
|---|---|
| Null or empty bank array | Return 0 if the input bank array is null or empty. |
| Bank array contains only empty rows (all zeros) | The algorithm should correctly handle all zero rows, skipping them in the calculation without causing errors. |
| Bank array contains only one row | Return 0 since laser beams need at least two rows. |
| Very large bank array dimensions (potential integer overflow) | Use a data type that can accommodate large numbers of beams to avoid integer overflow, such as long. |
| Rows with a very large number of devices (1s), close to the maximum integer limit | Ensure the counting mechanism for devices in a row doesn't cause integer overflow; use long if necessary. |
| The input bank array consists of rows that alternate between having zero and non-zero devices. | The algorithm should still correctly compute beams between these sparse layers. |
| Input array contains non-binary digits other than '0' or '1' | Filter out non-binary digits or throw an error to indicate invalid input. |
| Extremely long rows with all 1s | Efficiently count 1s in the row to avoid performance bottlenecks. |