From 2daade49c0847484e1e55d2402739f6d818ddb7a Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 3 Jan 2024 12:05:21 +0100 Subject: [PATCH] =?UTF-8?q?cpp:=20add=20=E2=80=9C2125.=20Number=20of=20Las?= =?UTF-8?q?er=20Beams=20in=20a=20Bank=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cpp/number-of-laser-beams-in-a-bank.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cpp/number-of-laser-beams-in-a-bank.cpp diff --git a/cpp/number-of-laser-beams-in-a-bank.cpp b/cpp/number-of-laser-beams-in-a-bank.cpp new file mode 100644 index 0000000..c9bebc2 --- /dev/null +++ b/cpp/number-of-laser-beams-in-a-bank.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +class Solution { +public: + int numberOfBeams(const std::vector& 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; + } +}; +