leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit b4cb2b84000edff8d93699cde3c33998d60ad826
parent 1abf5ecf0e9c2ccf6a654bbb139c6a7802db7d1c
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  3 Jan 2024 16:33:48 +0000

Improved Daily Problem

Diffstat:
MProblems/2125.cpp | 19++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/Problems/2125.cpp b/Problems/2125.cpp @@ -1,19 +1,12 @@ -// 2125. Number of Laser Beams in a Bank class Solution { - public: - int numberOfBeams(const vector<string> &bank) { - vector<int> count; - count.reserve(bank.size()); - for (const auto &floor : bank) { +public: + int numberOfBeams(const vector<string>& bank) const { + int res = 0, prev = 0; + for(const auto& floor: bank) { int cnt = 0; - for (char c : floor) - if (c == '1') cnt++; - if (cnt) count.push_back(cnt); + for(const char c: floor) cnt += c == '1'; + if(cnt) res += cnt * prev, prev = cnt; } - - int res = 0; - for (int i = 1; i < count.size(); i++) - res += count[i] * count[i - 1]; return res; } };