leetcode

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

commit e48442b51718815055f3dce312c7b093b82b0c7e
parent af0d37d72a902e65b341d68f566217e8a1b31f53
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 20 Nov 2024 21:45:52 +0100

Daily Problem

Diffstat:
AProblems/2516.cpp | 24++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Problems/2516.cpp b/Problems/2516.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + int takeCharacters(const string &s, int k) const { + const int n = size(s); + int count[3] = {0}; + + for (const char c : s) + count[c - 'a']++; + if (count[0] < k || count[1] < k || count[2] < k) return -1; + + int res = n; + for (int i = n - 1, j = n - 1; i >= 0; i--) { + count[s[i] - 'a']--; + + while (count[0] < k || count[1] < k || count[2] < k) { + count[s[j--] - 'a']++; + } + + res = min(res, i - j); + } + + return res + n - 1; + } +}; diff --git a/README.md b/README.md @@ -1324,6 +1324,7 @@ reference and a base for solving problems. | 2497 | Medium | [Maximum Star Sum of a Graph](Problems/2497.cpp) | | 2498 | Medium | [Frog Jump II](Problems/2498.cpp) | | 2501 | Medium | [Longest Square Streak in an Array](Problems/2501.cpp) | +| 2516 | Medium | [Take K of Each Character From Left and Right](Problems/2516.cpp) | | 2517 | Medium | [Maximum Tastiness of Candy Basket](Problems/2517.cpp) | | 2526 | Medium | [Find Consecutive Integers from a Data Stream](Problems/2526.cpp) | | 2527 | Medium | [Find Xor-Beauty of Array](Problems/2527.cpp) |