leetcode

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

commit d48779f3d8402685b15837499abe366a0230a0cb
parent 079f8655a3505f5ecd5b5756ffc201133955856c
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  3 Oct 2024 22:25:12 +0200

Daily Problem

Diffstat:
AProblems/1590.cpp | 26++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/Problems/1590.cpp b/Problems/1590.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + int minSubarray(const vector<int> &nums, int p) const { + const int n = size(nums); + + int target = 0; + for (const int n : nums) + target = (target + n) % p; // avoid overflow + if (target == 0) return 0; + + unordered_map<int, int> um; + int res = n, crnt = 0; + um[0] = -1; + + for (int i = 0; i < n; i++) { + crnt = (crnt + nums[i]) % p; + + const int goal = (crnt - target + p) % p; + if (um.count(goal)) res = min(res, i - um[goal]); + + um[crnt] = i; + } + + return res == n ? -1 : res; + } +}; diff --git a/README.md b/README.md @@ -917,6 +917,7 @@ for solving problems. | 1583 | Medium | [Count Unhappy Friends](Problems/1583.cpp) | | 1584 | Medium | [Min Cost to Connect All Points](Problems/1584.cpp) | | 1587 | Easy | [Bank Account Summary II](Problems/1587.cpp) | +| 1590 | Medium | [Make Sum Divisible by P](Problems/1590.cpp) | | 1593 | Medium | [Split a String Into the Max Number of Unique Substrings](Problems/1593.cpp) | | 1598 | Easy | [Crawler Log Folder](Problems/1598.cpp) | | 1600 | Medium | [Throne Inheritance](Problems/1600.cpp) |