Skip to content

Commit 3a3626c

Browse files
authored
Create number-of-laser-beams-in-a-bank.cpp
1 parent 0505d28 commit 3a3626c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(m * n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int numberOfBeams(vector<string>& bank) {
7+
int result = 0, prev = 0;
8+
for (const auto& x : bank) {
9+
const auto& cnt = count(cbegin(x), cend(x), '1');
10+
if (!cnt) {
11+
continue;
12+
}
13+
result += prev * cnt;
14+
prev = cnt;
15+
}
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)