Leetcode 2125: Number of Laser Beams in a Bank

grid47
grid47
Exploring patterns and algorithms
Apr 8, 2024 5 min read

You are given a 2D binary grid, where ‘1’ represents a security device and ‘0’ represents an empty cell. A laser beam can be formed between two devices if they are located on two different rows and no security device is in any row between them. The task is to find the total number of laser beams that can be formed.
Problem
Approach
Steps
Complexity
Input: You are given a grid of '1's and '0's where each string in the array represents a row in the grid.
Example: bank = ["10101", "00000", "10101"]
Constraints:
• 1 <= m, n <= 500
• Each element of bank is either '0' or '1'.
Output: Return the total number of laser beams that can be formed.
Example: Input: bank = ["10101", "00000", "10101"] Output: 4
Constraints:
• m == bank.length
• n == bank[i].length
• bank[i][j] is either '0' or '1'.
Goal: Count the number of laser beams that can be formed by finding pairs of security devices that are on different rows with no devices in between.
Steps:
• For each row in the grid, count the number of security devices.
• For each pair of rows with security devices, compute the number of beams as the product of the device counts in those rows.
Goal: The grid size can be as large as 500x500.
Steps:
• The number of rows and columns can be between 1 and 500.
• The grid consists only of '0's and '1's.
Assumptions:
• The grid may have rows or columns that are completely empty of security devices.
Input: Input: bank = ["011001", "000000", "010100", "001000"]
Explanation: The laser beams can be formed between devices on different rows, without any devices in between rows. The total number of beams is 8.

Input: Input: bank = ["000", "111", "000"]
Explanation: No beams can be formed because the devices are on the same row, and there are no devices on different rows.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus