leetcode

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

commit c6717caa4cfca3b3ed10ea3c4bb2862da128e97a
parent ad38e7fa47b37eb6812012b161450d1cb491ed96
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 27 Mar 2024 15:27:00 +0000

Daily Problem

Diffstat:
AProblems/2958.cpp | 17+++++++++++++++++
MREADME.md | 1+
2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/Problems/2958.cpp b/Problems/2958.cpp @@ -0,0 +1,17 @@ +class Solution { + public: + int maxSubarrayLength(const vector<int> &nums, const int k) const { + unordered_map<int, int> freq; + const int n = size(nums); + int res = 0, i = 0; + + for (int j = 0; j < n; j++) { + freq[nums[j]]++; + while (freq[nums[j]] > k) + freq[nums[i++]]--; + res = max(res, j - i + 1); + } + + return max(res, n - i); + } +}; diff --git a/README.md b/README.md @@ -1168,6 +1168,7 @@ for solving problems. | 2938 | Medium | [Separate Black and White Balls](Problems/2938.cpp) | | 2947 | Medium | [Count Beautiful Substrings I](Problems/2947.cpp) | | 2952 | Medium | [Minimum Number of Coins to be Added](Problems/2952.cpp) | +| 2958 | Medium | [Length of Longest Subarray With at Most K Frequency](Problems/2958.cpp) | | 2966 | Medium | [Divide Array Into Arrays With Max Difference](Problems/2966.cpp) | | 2971 | Medium | [Find Polygon With the Largest Perimeter](Problems/2971.cpp) | | 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) |