✅Beats 99.01% - Explained with [ Video ] - C++/Java/Python/JS - Iterate and Count
\n\n# YouTube Video Explanation:\n\n **If you want a video for this question please write in the comments** \n\n https://www.youtube.com/watch?v=ujU-jeO1v-k \nh
![Screenshot 2024-01-03 061255.png](https://assets.leetcode.com/users/images/aa17a167-24e2-4f0b-8827-43d599bbadda_1704242635.0197337.png)\n\n# YouTube Video Explanation:\n\n<!-- **If you want a video for this question please write in the comments** -->\n\n<!-- https://www.youtube.com/watch?v=ujU-jeO1v-k -->\nhttps://youtu.be/WIAeWzSG1ts\n\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\n\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\n\n*Subscribe Goal: 1200 Subscribers*\n*Current Subscribers: 1105*\n\n---\n\n\n# Intuition\nThe problem involves counting the number of laser beams that can be formed between pairs of security devices in the bank. A laser beam is possible between two devices if there are no security devices in the rows between them.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Iterate through each row of the bank and calculate the number of security devices in that row (`curRowCount`).\n2. For each non-empty row, calculate the number of laser beams that can be formed between devices in the current row and devices in the previous row (`prevRowCount`).\n3. Update the total count of laser beams.\n4. Return the total count.\n\n# Complexity\n- Time complexity: O(m * n), where m is the number of rows and n is the number of columns in the bank. The solution iterates through each row and calculates the count of security devices in that row.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1), as the space used is constant, and no additional data structures are used.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int prevRowCount = 0;\n        int total=0;\n\n        for(String row : bank) {\n            int curRowCount = calc(row);\n            if(curRowCount==0) \n                continue;\n\n            total += curRowCount * prevRowCount;\n            prevRowCount = curRowCount;\n        }\n        return total;\n    }\n\n    private int calc(String s) {\n        int count = 0;\n        for(char c : s.toCharArray()) \n            count += c - \'0\';\n\n        return count;\n    }    \n}\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfBeams(std::vector<std::string>& bank) {\n        int prevRowCount = 0;\n        int total = 0;\n\n        for (const std::string& row : bank) {\n            int curRowCount = calc(row);\n            if (curRowCount == 0)\n                continue;\n\n            total += curRowCount * prevRowCount;\n            prevRowCount = curRowCount;\n        }\n        return total;\n    }\n\nprivate:\n    int calc(const std::string& s) {\n        int count = 0;\n        for (char c : s)\n            count += c - \'0\';\n\n        return count;\n    }\n};\n```\n```Python []\nclass Solution(object):\n    def numberOfBeams(self, bank):\n        prev_row_count = 0\n        total = 0\n\n        for row in bank:\n            cur_row_count = self.calc(row)\n            if cur_row_count == 0:\n                continue\n\n            total += cur_row_count * prev_row_count\n            prev_row_count = cur_row_count\n\n        return total\n\n    def calc(self, s):\n        return sum(int(c) for c in s)\n        \n```\n```JavaScript []\n/**\n * @param {string[]} bank\n * @return {number}\n */\nvar numberOfBeams = function(bank) {\n    let prevRowCount = 0;\n    let total = 0;\n\n    const calc = (s) => {\n        return s.split(\'\').reduce((count, c) => count + parseInt(c), 0);\n    };\n\n    for (const row of bank) {\n        const curRowCount = calc(row);\n        if (curRowCount === 0)\n            continue;\n\n        total += curRowCount * prevRowCount;\n        prevRowCount = curRowCount;\n    }\n    return total;\n};\n```\n![upvote.png](https://assets.leetcode.com/users/images/5772dfc2-e013-4ddb-aaef-bc69fe88bf65_1704242539.994013.png)\n
['Array', 'Math', 'String', 'Matrix', 'Python', 'C++', 'Java', 'JavaScript']