mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
cpp: add “2125. Number of Laser Beams in a Bank”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f8f23de17e
commit
2daade49c0
1 changed files with 21 additions and 0 deletions
21
cpp/number-of-laser-beams-in-a-bank.cpp
Normal file
21
cpp/number-of-laser-beams-in-a-bank.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int numberOfBeams(const std::vector<std::string>& bank) {
|
||||
int beams = 0;
|
||||
|
||||
int last_row = 0;
|
||||
for (const auto& row : bank) {
|
||||
if (auto current_row = std::count(row.begin(), row.end(), '1'); current_row != 0) {
|
||||
beams += last_row * current_row;
|
||||
last_row = current_row;
|
||||
}
|
||||
}
|
||||
|
||||
return beams;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in a new issue