✅🔥 Easy and Simple 😎 Simple Math + O(1) Space 🚀 Faster than 100 % 🔥
Please Upvote if you liked my Solution \uD83E\uDD17\uD83E\uDD17\n> Please do UpVote it took lot of time to figure out these solutions\n\n# Approach\nFor ith row
# Please Upvote if you liked my Solution \uD83E\uDD17\uD83E\uDD17\n> **Please do UpVote** it took lot of time to figure out these solutions\n\n# Approach\nFor ith row, find previous count of security devices. If it\'s non-zero then increment ans. \n\n# Complexity \n- Time complexity: $$O(N*M)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: $$O(1)$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nwhere `N = bank.length` and `M = bank[0].length` \n\n\n# C++ Code\n```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int ans = 0, prev = 0;\n        \n        for(auto &s : bank){\n            int cnt = 0;\n            for(auto c : s) cnt += (c==\'1\');\n\n            if(cnt) ans += prev*cnt, prev = cnt;\n        }\n\n        return ans;\n    }\n};\n```\n\n---\n\n# Java Code\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int ans = 0, prev=0;\n        \n        for(String s : bank){\n            int cnt = 0;\n            for(char c : s.toCharArray()){\n                if( c==\'1\' ) cnt++;\n            }\n\n            if(cnt != 0){ \n                ans += prev*cnt; \n                prev = cnt;\n            } \n        }\n\n        return ans;\n    }\n}\n```\n\n![upvote-4.png](https://assets.leetcode.com/users/images/5af4e2a5-e8b2-4f46-be00-75247cfdf9ff_1689947516.7818904.png)\n
['Array', 'Math', 'String', 'Matrix', 'C++', 'Java']