leetcode

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

commit fc4d87afe2816ede73fa6d7daeb3dc887c7ac0e3
parent f81d63d0a6b0e03385c36bab57b8e392203bfddd
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 19 Jan 2023 14:28:43 +0100

Daily Problem

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

diff --git a/Problems/0974.cpp b/Problems/0974.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int subarraysDivByK(vector<int> &nums, int k) { + int n = nums.size(), sum = 0, res = 0; + vector<int> dp(k, 0); + + dp[0] = 1; + for (int num : nums) { + sum += num; + int rem = sum % k; + if (rem < 0) rem += k; + res += dp[rem]++; + } + return res; + } +}; diff --git a/README.md b/README.md @@ -218,6 +218,7 @@ for solving problems. | 0950 | Medium | [Reveal Cards In Increasing Order](Problems/0950.cpp) | | 0959 | Medium | [Regions Cut By Slashes](Problems/0959.cpp) | | 0965 | Easy | [Univalued Binary Tree](Problems/0965.cpp) | +| 0974 | Medium | [Subarray Sums Divisible by K](Problems/0974.cpp) | | 0977 | Easy | [Squares of a Sorted Array](Problems/0977.cpp) | | 0980 | Hard | [Unique Paths III](Problems/0980.cpp) | | 0989 | Easy | [Add to Array-Form of Integer](Problems/0989.cpp) |