{"id":9398,"date":"2022-01-02T00:22:50","date_gmt":"2022-01-02T08:22:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9398"},"modified":"2022-01-02T00:57:04","modified_gmt":"2022-01-02T08:57:04","slug":"leetcode-2125-number-of-laser-beams-in-a-bank","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2125-number-of-laser-beams-in-a-bank\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2125. Number of Laser Beams in a Bank"},"content":{"rendered":"\n<p>Anti-theft security devices are activated inside a bank. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;binary string array&nbsp;<code>bank<\/code>&nbsp;representing the floor plan of the bank, which is an&nbsp;<code>m x n<\/code>&nbsp;2D matrix.&nbsp;<code>bank[i]<\/code>&nbsp;represents the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;row, consisting of&nbsp;<code>'0'<\/code>s and&nbsp;<code>'1'<\/code>s.&nbsp;<code>'0'<\/code>&nbsp;means the cell is empty, while<code>'1'<\/code>&nbsp;means the cell has a security device.<\/p>\n\n\n\n<p>There is&nbsp;<strong>one<\/strong>&nbsp;laser beam between any&nbsp;<strong>two<\/strong>&nbsp;security devices&nbsp;<strong>if both<\/strong>&nbsp;conditions are met:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The two devices are located on two&nbsp;<strong>different rows<\/strong>:&nbsp;<code>r<sub>1<\/sub><\/code>&nbsp;and&nbsp;<code>r<sub>2<\/sub><\/code>, where&nbsp;<code>r<sub>1<\/sub>&nbsp;&lt; r<sub>2<\/sub><\/code>.<\/li><li>For&nbsp;<strong>each<\/strong>&nbsp;row&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>r<sub>1<\/sub>&nbsp;&lt; i &lt; r<sub>2<\/sub><\/code>, there are&nbsp;<strong>no security devices<\/strong>&nbsp;in the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;row.<\/li><\/ul>\n\n\n\n<p>Laser beams are independent, i.e., one beam does not interfere nor join with another.<\/p>\n\n\n\n<p>Return&nbsp;<em>the total number of laser beams in the bank<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/24\/laser1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> bank = [\"011001\",\"000000\",\"010100\",\"001000\"]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> Between each of the following device pairs, there is one beam. In total, there are 8 beams:\n * bank[0][1] -- bank[2][1]\n * bank[0][1] -- bank[2][3]\n * bank[0][2] -- bank[2][1]\n * bank[0][2] -- bank[2][3]\n * bank[0][5] -- bank[2][1]\n * bank[0][5] -- bank[2][3]\n * bank[2][1] -- bank[3][2]\n * bank[2][3] -- bank[3][2]\nNote that there is no beam between any device on the 0<sup>th<\/sup> row with any on the 3<sup>rd<\/sup> row.\nThis is because the 2<sup>nd<\/sup> row contains security devices, which breaks the second condition.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/24\/laser2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> bank = [\"000\",\"111\",\"000\"]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There does not exist two devices located on two different rows.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == bank.length<\/code><\/li><li><code>n == bank[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 500<\/code><\/li><li><code>bank[i][j]<\/code>&nbsp;is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Rule of product<\/strong><\/h2>\n\n\n\n<p>Just need to remember the # of devices of prev non-empty row.<br># of beams between two non-empty row equals to row[i] * row[j]<br>ans += prev * curr<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfBeams(vector<string>& bank) {    \n    int ans = 0;\n    int prev = 0;\n    for (const string& row : bank)    \n      if (int curr = count(begin(row), end(row), '1')) {\n        ans += prev * curr;\n        prev = curr;\n      }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def numberOfBeams(self, bank: List[str]) -> int:\n    ans = prev = 0\n    for row in bank:\n      if curr := row.count('1'):\n        ans += prev * curr\n        prev = curr\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Anti-theft security devices are activated inside a bank. You are given a&nbsp;0-indexed&nbsp;binary string array&nbsp;bank&nbsp;representing the floor plan of the bank, which is an&nbsp;m x n&nbsp;2D&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,216,177,4],"class_list":["post-9398","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-matrix","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9398"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9398\/revisions"}],"predecessor-version":[{"id":9405,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9398\/revisions\/9405"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}