Number of Laser Beams in a Bank Solution in C++
Intuition\n Describe your first thoughts on how to solve this problem. \nIteration\n# Approach\n Describe your approach to solving the problem. \n1. We will fir
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIteration\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We will firstly see if a row has \'1\' or not. \n2. If it has, then we see whether that\'s the first row to have \'1\'.\n3. If yes, them we calculate the number of \'1\'\'s in the row and store in variable m.\n4. We then check for other rows iteratively, if they have \'1\', then the count of \'1\'s is calculated for the row and multiplied by the intitial value \'m\'.\n5. We then update the value of \'m\' to the current count of \'1\'s in the present row.\n6. Continue this process till we have traversed all the rows.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n^2)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int i, m=0, total_laser_beams=0;\n        for(i=0 ; i<bank.size() ; i++)\n        {\n            if(count(bank[i].begin(), bank[i].end(), \'1\')>0)\n            {\n                if(m==0)\n                {\n                    m = count(bank[i].begin(), bank[i].end(), \'1\');\n                }\n                else\n                {\n                    total_laser_beams += m*count(bank[i].begin(), bank[i].end(), \'1\');\n                    m = count(bank[i].begin(), bank[i].end(), \'1\');\n                }\n            }\n        }\n        return total_laser_beams;\n    }\n};\n```\n![upvote new.jpg](https://assets.leetcode.com/users/images/930cfcc0-9885-4143-90c7-3d9242b182ae_1682613170.83302.jpeg)\n
['C++']