leetcode

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

commit 11545c37d7846e87a2fffced3b271256bd672e5f
parent 52df518d7bee5c4a6945fb828eae99ced27a7a75
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 10 Dec 2024 13:18:23 +0100

Daily Problem, 1 Random

Diffstat:
AProblems/2981.cpp | 28++++++++++++++++++++++++++++
AProblems/2982.cpp | 28++++++++++++++++++++++++++++
MREADME.md | 2++
3 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/Problems/2981.cpp b/Problems/2981.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + int maximumLength(const string &s) const { + static int count[26][3]; + int cnt = 0, prev = s[0]; + + memset(count, 0xFF, sizeof(count)); + for (const char c : s) { + if (c == prev) + cnt++; + else { + prev = c; + cnt = 1; + } + + const int idx = c - 'a'; + auto mini = min_element(count[idx], count[idx] + 3); + if (cnt > *mini) *mini = cnt; + } + + int res = -1; + for (int i = 0; i < 26; i++) { + res = max(res, *min_element(count[i], count[i] + 3)); + } + + return res; + } +}; diff --git a/Problems/2982.cpp b/Problems/2982.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + int maximumLength(const string &s) const { + static int count[26][3]; + int cnt = 0, prev = s[0]; + + memset(count, 0xFF, sizeof(count)); + for (const char c : s) { + if (c == prev) + cnt++; + else { + prev = c; + cnt = 1; + } + + const int idx = c - 'a'; + auto mini = min_element(count[idx], count[idx] + 3); + if (cnt > *mini) *mini = cnt; + } + + int res = -1; + for (int i = 0; i < 26; i++) { + res = max(res, *min_element(count[i], count[i] + 3)); + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -1441,6 +1441,8 @@ reference and a base for solving problems. | 2966 | Medium | [Divide Array Into Arrays With Max Difference](Problems/2966.cpp) | | 2971 | Medium | [Find Polygon With the Largest Perimeter](Problems/2971.cpp) | | 2976 | Medium | [Minimum Cost to Convert String I](Problems/2976.cpp) | +| 2981 | Medium | [Find Longest Special Substring That Occurs Thrice I](Problems/2981.cpp) | +| 2982 | Medium | [Find Longest Special Substring That Occurs Thrice II](Problems/2982.cpp) | | 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) | | 3005 | Easy | [Count Elements With Maximum Frequency](Problems/3005.cpp) | | 3011 | Medium | [Find if Array Can Be Sorted](Problems/3011.cpp) |