java.lang.Object
g2101_2200.s2125_number_of_laser_beams_in_a_bank.Solution

public class Solution extends Object
2125 - Number of Laser Beams in a Bank.<p>Medium</p> <p>Anti-theft security devices are activated inside a bank. You are given a <strong>0-indexed</strong> binary string array <code>bank</code> representing the floor plan of the bank, which is an <code>m x n</code> 2D matrix. <code>bank[i]</code> represents the <code>i<sup>th</sup></code> row, consisting of <code>'0'</code>s and <code>'1'</code>s. <code>'0'</code> means the cell is empty, while<code>'1'</code> means the cell has a security device.</p> <p>There is <strong>one</strong> laser beam between any <strong>two</strong> security devices <strong>if both</strong> conditions are met:</p> <ul> <li>The two devices are located on two <strong>different rows</strong>: <code>r<sub>1</sub></code> and <code>r<sub>2</sub></code>, where <code>r<sub>1</sub> < r<sub>2</sub></code>.</li> <li>For <strong>each</strong> row <code>i</code> where <code>r<sub>1</sub> < i < r<sub>2</sub></code>, there are <strong>no security devices</strong> in the <code>i<sup>th</sup></code> row.</li> </ul> <p>Laser beams are independent, i.e., one beam does not interfere nor join with another.</p> <p>Return <em>the total number of laser beams in the bank</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg" alt="" /></p> <p><strong>Input:</strong> bank = [&ldquo;011001&rdquo;,&ldquo;000000&rdquo;,&ldquo;010100&rdquo;,&ldquo;001000&rdquo;]</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> Between each of the following device pairs, there is one beam. In total, there are 8 beams:</p> <ul> <li> <p>bank[0][1] &ndash; bank[2][1]</p> </li> <li> <p>bank[0][1] &ndash; bank[2][3]</p> </li> <li> <p>bank[0][2] &ndash; bank[2][1]</p> </li> <li> <p>bank[0][2] &ndash; bank[2][3]</p> </li> <li> <p>bank[0][5] &ndash; bank[2][1]</p> </li> <li> <p>bank[0][5] &ndash; bank[2][3]</p> </li> <li> <p>bank[2][1] &ndash; bank[3][2]</p> </li> <li> <p>bank[2][3] &ndash; bank[3][2]</p> </li> </ul> <p>Note that there is no beam between any device on the 0<sup>th</sup> row with any on the 3<sup>rd</sup> row.</p> <p>This is because the 2<sup>nd</sup> row contains security devices, which breaks the second condition.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg" alt="" /></p> <p><strong>Input:</strong> bank = [&ldquo;000&rdquo;,&ldquo;111&rdquo;,&ldquo;000&rdquo;]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There does not exist two devices located on two different rows.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == bank.length</code></li> <li><code>n == bank[i].length</code></li> <li><code>1 <= m, n <= 500</code></li> <li><code>bank[i][j]</code> is either <code>'0'</code> or <code>'1'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfBeams

      public int numberOfBeams(String[] bank)