✅Beats 100% Users - C++, JAVA, Python, JavaScript || With explanation ||
Please Upvote if you find it is helpfull.\n\n---\n\n\n---\n\n\n# Approach\n Describe your approach to solving the problem. \n1. Input Validation:\n   - The code
# **Please Upvote if you find it is helpfull.**\n\n---\n![Screenshot 2024-01-03 070619.png](https://assets.leetcode.com/users/images/19856c92-4075-4ca4-848f-f274ff66751b_1704245807.7514522.png)\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1. Input Validation:**\n   - The code checks for invalid input (less than two rows in `bank`) and returns 0 in those cases.\n\n**2. Initialization:**\n   - It initializes variables to track the number of beams (`solution`), beacons in the previous row (`beaconsInPrevRow`), and beacons in the current row (`beaconsInCurrentRow`).\n\n**3. Row Iteration:**\n   - It iterates through each row of the `bank` vector.\n\n**4. Beacon Counting:**\n   - For each row, it counts the number of \'1\' characters, representing beacons.\n\n**5. Beam Calculation:**\n   - It calculates the number of beams for the current row by multiplying the number of beacons in the current row with the number of beacons in the previous row.\n   - It adds this value to the `solution` variable.\n\n**6. Updating Previous Row Beacons:**\n   - It updates `beaconsInPrevRow` with the number of beacons in the current row, preparing for the next iteration.\n\n**Time Complexity:**\n   - O(R * C), where R is the number of rows and C is the maximum string length in `bank`.\n     - The nested loops iterate through each element of the 2D string representation.\n\n**Space Complexity:**\n   - O(1), constant space.\n     - The code uses a few variables for calculations, but their space usage doesn\'t depend on the input size.\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) \n    {        \n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n\n        if(bank.size() < 2)\n            return 0;\n\n        int solution = 0;\n        short beaconsInPrevRow = 0;\n        short beaconsInCurrentRow = 0;\n        for(size_t i = 0; i < bank.size(); ++i)\n        {\n            beaconsInCurrentRow = 0;\n\n            for(auto &it : bank[i])\n            {\n                if(it == \'1\')\n                {\n                    beaconsInCurrentRow++;\n                }\n            }\n            solution += beaconsInCurrentRow * beaconsInPrevRow;\n            beaconsInPrevRow = (beaconsInCurrentRow == 0) * beaconsInPrevRow + beaconsInCurrentRow;\n        }\n\n        return solution;\n    }\n};\n```\n```JAVA []\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        if (bank.length < 2) {\n            return 0;\n        }\n\n        int solution = 0;\n        int beaconsInPrevRow = 0;\n        int beaconsInCurrentRow = 0;\n        for (String row : bank) {\n            beaconsInCurrentRow = 0;\n            for (char c : row.toCharArray()) {\n                if (c == \'1\') {\n                    beaconsInCurrentRow++;\n                }\n            }\n            solution += beaconsInCurrentRow * beaconsInPrevRow;\n            beaconsInPrevRow = beaconsInCurrentRow == 0 ? beaconsInPrevRow : beaconsInCurrentRow;\n        }\n\n        return solution;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def numberOfBeams(self, bank: List[str]) -> int:\n        if len(bank) < 2:\n            return 0\n\n        solution = 0\n        beacons_in_prev_row = 0\n        beacons_in_current_row = 0\n        for row in bank:\n            beacons_in_current_row = 0\n            for c in row:\n                if c == \'1\':\n                    beacons_in_current_row += 1\n            solution += beacons_in_current_row * beacons_in_prev_row\n            beacons_in_prev_row = beacons_in_current_row or beacons_in_prev_row\n\n        return solution\n\n```\n```javascript []\nclass Solution {\n    numberOfBeams(bank) {\n        if (bank.length < 2) {\n            return 0;\n        }\n\n        let solution = 0;\n        let beaconsInPrevRow = 0;\n        let beaconsInCurrentRow = 0;\n        for (const row of bank) {\n            beaconsInCurrentRow = 0;\n            for (const c of row) {\n                if (c === \'1\') {\n                    beaconsInCurrentRow++;\n                }\n            }\n            solution += beaconsInCurrentRow * beaconsInPrevRow;\n            beaconsInPrevRow = beaconsInCurrentRow ? beaconsInCurrentRow : beaconsInPrevRow;\n        }\n\n        return solution;\n    }\n}\n\n```\n\n
['Array', 'Math', 'String', 'Matrix', 'Python', 'C++', 'Java', 'JavaScript']