Number of Laser Beams in a Bank - C++ Solution - ✅Beats 99.90%
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to count the number of laser beams that can be formed between pairs o
![Screenshot 2024-01-03 132003.png](https://assets.leetcode.com/users/images/8b0257cd-f81a-4ba8-a77f-ef8dba2dfd70_1704268226.886636.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to count the number of laser beams that can be formed between pairs of security devices in a bank. The laser beams can only exist between devices on different rows, with no security devices in the rows in between. The given bank is represented as a binary matrix, where \'1\' represents a security device, and \'0\' represents an empty cell.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Define a helper function `Countones` to count the number of security devices in a given row.\n2. Iterate through each row of the bank and count the number of security devices in each row. Store these counts in a vector called `rows`.\n3. Remove rows with no security devices (rows with count \'0\') from the `rows` vector.\n4. If the size of the `rows` vector is less than or equal to 1, return 0, as there are not enough rows with security devices to form laser beams.\n5. Iterate through the `rows` vector, and for each pair of consecutive rows, calculate the number of laser beams that can be formed between them. Add this count to the final answer.\n6. Return the total number of laser beams.\n\n# Complexity\n### Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nLet m be the number of rows and n be the number of columns in the bank.\n1. Counting the number of security devices in a row (`Countones` function): O(n)\n2. Iterating through each row of the bank to count security devices and calculating the total number of laser beams: O(m * n)\n3. Removing rows with no security devices from the `rows` vector: O(m)\n4. Iterating through the `rows` vector to calculate the total number of laser beams: O(m)\n\nThe overall time complexity is `O(m * n)`.\n\n### Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1. `rows` vector: O(m) - to store the count of security devices in each row.\n2. Other variables: O(1)\n\nThe overall space complexity is `O(m)`.\n\n---\n**Elasped Time:** 38 ms\n**Speed Record:** 99.90%\n**Memory:** 23.30MB\n\n---\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int Countones(string &singlerowofbank)\n    {\n        int cnt = 0;\n        for(auto ele:singlerowofbank)\n            if(ele == \'1\')\n                cnt++;\n        return cnt;\n    }\n\n    int numberOfBeams(vector<string>& bank) {\n        std::ios::sync_with_stdio(false);\n        cin.tie(NULL);\n        vector<int> rows;\n        int ans = 0;\n        for(string&singlerowofbank : bank)\n        {\n            rows.push_back(Countones(singlerowofbank));\n        }\n        rows.erase(remove(rows.begin(), rows.end(), 0), rows.end());\n        if(rows.size()<=1)  return 0;\n        for(int i=0;i<rows.size()-1;i++)\n        {\n            ans+=rows[i]*rows[i+1];\n        }\n        return ans;\n    }\n};\n```\n![images.jpeg](https://assets.leetcode.com/users/images/a04f751f-3b1e-4618-b822-220f4ac92290_1704268153.423459.jpeg)\n
['Math', 'String', 'Matrix', 'C++']