Count 1's and Multiply | C++ | Explained
\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        \n        vector<int> num;  // to store the camera cnt at each lvl if cnt>0\
```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        \n        vector<int> num;  // to store the camera cnt at each lvl if cnt>0\n        int cnt=0;\n\t\t\tfor(int i=0;i<bank.size();i++){  // counting camera at all lvl\n            cnt=0;\n            for(int j=0;j<bank[0].size();j++){\n                if(bank[i][j]==\'1\'){\n                    cnt++;\n                }                \n            }\n            if(cnt>0)  //only need to push if cnt>0\n                num.push_back(cnt);\n        }\n        \n        if(num.size()<=1){  //corner case\n            return 0;\n        }\n\t\t\n        long long sum=0 ;\n        for(int i=0;i<num.size()-1;i++){   // calc final ans\n            sum+=(num[i]*num[i+1]);\n        }\n        \n        return sum;\n        \n    }\n};\n```\nConsider any two levels with camera\'s, and observe that\n*  No. of laser b/w level i and i+1 = (num of camera on lvl  i) * (num of camera on lvl i+1)\n\n1. So keep count of camera in a vector ```num``` and multiply ``` num[i] * num[i+1]``` to get no. of lasers from lvl i to lvl i+1\n2.  Since you need count no of lasers between each level, keep on adding the product into ```sum``` to get total count\n\n**NOTE** ->  we only need to store the value in nums if the level has >0 cameras, since a laser extends to the next level having a camera. ( 0 value in num would spoil our ans ). \n\n**DRY RUN**\n1. For Eg1, our ```num``` will be  ```num={3,2,1}``` .\n2. For laser b/w r2 & r1 we have 3 * 2 = 6;\n3. For lasers b/w r2 & r3 we have 2 * 1 = 2;\n4. Final ```ans  = sum of all levels = 6+2 = 8 ```\n\n``` Time Complexity : O(n*m) , where n= no of rows and m = no of columns ```\n
['Math', 'C', 'C++']