[C++] Count 1, Skip Empty Row, Sum of Product
Intuition\n Describe your first thoughts on how to solve this problem. \n- Count the number of 1 (security devices) for each row\n- Skip the empty row(s)\n- The
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Count the number of `1` (security devices) for each row\n- Skip the empty row(s)\n- The answer is the sum of the product between all adjacent non-empty rows\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterate for each row\n  - get the number of devices in the current row\n  - skip the row without any devices\n  - update the result and the previous\n\n# Complexity\n- Time complexity: $$O({m}\\times{n})$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    // time/space: O(mn)/O(1)\n    int numberOfBeams(vector<string>& bank) {\n        int result = 0;\n        int prev = 0;\n        for (auto& row : bank) {\n            // get the number of devices in the current row\n            int curr = count(row.begin(), row.end(), \'1\');\n            // skip the row without any devices\n            if (curr == 0) continue;\n            // update the result and the previous\n            result += (prev * curr);\n            prev = curr;\n        }\n        return result;\n    }\n};\n```
['Math', 'String', 'Matrix', 'C++']