Explained Amazingly Well
Intuition\nThe problem is asking to count the number of "beams" in a given bank represented as a matrix of 0s and 1s. A "beam" is defined as a pair of 1s in the
# Intuition\nThe problem is asking to count the number of "beams" in a given bank represented as a matrix of 0s and 1s. A "beam" is defined as a pair of 1s in the same row with no 0s between them. To solve this problem, we need to identify pairs of adjacent 1s in each row and then count the number of such pairs in the entire bank matrix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize a vector v to store the count of 1s in each row of the bank matrix. Also, initialize a variable counter to keep track of the total number of beams.\n\n2. Iterate through the bank matrix to calculate the count of 1s in each row. For each row, use a loop to count the number of 1s present.\n\n3. Next, iterate through the vector v to calculate the number of beams. Start from the second row (i = 1) and, for each row i, check if there are no 1s in that row (v[i] == 0). If so, update the count of 1s in that row to be the same as the count in the previous row (v[i] = v[i - 1]). This step ensures that if a row has no 1s, it inherits the count of 1s from the previous row since a beam cannot exist in that row.\n\n4. If there are 1s in the current row (v[i] != 0), calculate the number of beams by multiplying the count of 1s in the current row (v[i]) with the count of 1s in the previous row (v[i - 1]) and add this value to the counter. This step ensures that for each row with 1s, we can form beams by pairing the 1s in that row with the 1s in the previous row.\n\n5. After iterating through the entire v vector, the counter will hold the total number of beams in the bank matrix.\n\n6. Finally, return the calculated counter.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(R * C), where R is the number of rows and C is the number of columns in the bank matrix. We have two nested loops: one to calculate the count of 1s in each row and the other to calculate the number of beams.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(R), where R is the number of rows in the bank matrix. The additional space is used to store the count of 1s in each row in the v vector.\n\n# Code\n```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        vector <int> v(bank.size(), 0);\n        int counter = 0;\n        for(int i = 0; i<bank.size(); i++){\n            for(int j = 0; j<bank[i].size(); j++){\n                if(bank[i][j] == \'1\') v[i] = v[i] + 1;\n            }\n        }\n  \n\n        for(int i = 1; i<v.size(); i++){\n            if(v[i] == 0){\n                v[i] = v[i-1];\n                \n            }\n            else{\n                counter += (v[i-1] * v[i]);\n            }\n        }\n        return counter;\n    }\n};\n```
['C++']