leetcode

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

commit c15945ee7151880053e6b5921bc329d6025833b8
parent 9276bc698adfdf18fb16c9902f410e91508bde24
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 19 Jun 2024 21:13:55 +0200

1 Random Problem

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

diff --git a/Problems/1865.cpp b/Problems/1865.cpp @@ -0,0 +1,30 @@ +class FindSumPairs { + unordered_map<int, int> count1, count2; + vector<int> arr; + + public: + FindSumPairs(const vector<int> &nums1, const vector<int> &nums2) : arr(nums2) { + for (const int n : nums1) + count1[n]++; + for (const int n : nums2) + count2[n]++; + } + + void add(int index, int val) { + count2[arr[index]]--; + arr[index] += val; + count2[arr[index]]++; + } + + int count(int tot) const { + int res = 0; + + for (const auto [k, v] : count1) { + const auto it = count2.find(tot - k); + if (!v || it == count2.end()) continue; + res += v * it->second; + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -990,6 +990,7 @@ for solving problems. | 1860 | Medium | [Incremental Memory Leak](Problems/1860.cpp) | | 1861 | Medium | [Rotating the Box](Problems/1861.cpp) | | 1863 | Easy | [Sum of All Subset XOR Totals](Problems/1863.cpp) | +| 1865 | Medium | [Finding Pairs With a Certain Sum](Problems/1865.cpp) | | 1870 | Medium | [Minimum Speed to Arrive on Time](Problems/1870.cpp) | | 1873 | Easy | [Calculate Special Bonus](Problems/1873.cpp) | | 1877 | Medium | [Minimize Maximum Pair Sum in Array](Problems/1877.cpp) |