leetcode

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

commit 684821dab63d29da84aaaef826d95a007076c84f
parent 6b5625fa8ea8aab3ef9f6b675f8a29fc8a275614
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 10 Nov 2024 20:58:27 +0100

Daily Problem

Diffstat:
AProblems/3097.cpp | 31+++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/Problems/3097.cpp b/Problems/3097.cpp @@ -0,0 +1,31 @@ +class Solution { + inline static int update(int count[32], int num, int value) { + int crnt = 0; + + for (int k = 0; k < 32; k++) { + const auto bit = 1 << k; + if (num & bit) count[k] += value; + if (count[k]) crnt |= bit; + } + + return crnt; + } + + public: + int minimumSubarrayLength(const vector<int> &nums, int k) const { + static int count[32]; + unsigned res = -1; + + memset(count, 0x00, sizeof(count)); + for (int i = 0, j = 0; j < size(nums); j++) { + int crnt = update(count, nums[j], 1); + + for (; i <= j && crnt >= k; i++) { + res = min(res, (unsigned)(j - i + 1)); + crnt = update(count, nums[i], -1); + } + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -1427,6 +1427,7 @@ reference and a base for solving problems. | 3070 | Medium | [Count Submatrices with Top-Left Element and Sum Less Than k](Problems/3070.cpp) | | 3075 | Medium | [Maximize Happiness of Selected Children](Problems/3075.cpp) | | 3080 | Medium | [Mark Elements on Array by Performing Queries](Problems/3080.cpp) | +| 3097 | Medium | [Shortest Subarray With OR at Least K II](Problems/3097.cpp) | | 3100 | Medium | [Water Bottles II](Problems/3100.cpp) | | 3101 | Medium | [Count Alternating Subarrays](Problems/3101.cpp) | | 3106 | Medium | [Lexicographically Smallest String After Operations With Constraint](Problems/3106.cpp) |