Number of Laser Beams in a Bank

Medium Topics Company Tags

The equal-length strings in bank describe rows of cells. A 1 marks a security device and a 0 marks an empty cell. Two devices form one laser beam exactly when they are on different rows and every row strictly between them contains no devices. Their columns do not matter, and beams do not block each other. Return the total number of beams.

Filled circles mark security devices; dashed lines connect devices with laser beams.

Example 1:

Number of Laser Beams in a Bank, example 1: illustration of the example

Input: bank = ["101","000","011"]

Output: 4

Explanation: The first and last rows each have two devices, with an empty row between them. Every device in the first row connects to both in the last row, giving 2 times 2 = 4 beams.

Example 2:

Number of Laser Beams in a Bank, example 2: illustration of the example

Input: bank = ["111"]

Output: 0

Explanation: All devices are in the same row, so no pair meets the different-row condition.

Example 3:

Number of Laser Beams in a Bank, example 3: illustration of the example

Input: bank = ["10","01","10"]

Output: 2

Explanation: There is one beam from row 0 to row 1 and one from row 1 to row 2. Row 1 contains a device, so row 0 cannot connect directly to row 2.

Constraints:

  • 1 <= bank.length <= 500
  • 1 <= bank[i].length <= 500
  • All rows have the same length.
  • Each character is 0 or 1.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.