leetcode

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

1865.cpp (708B)


0 class FindSumPairs { 1 unordered_map<int, int> count1, count2; 2 vector<int> arr; 3 4 public: 5 FindSumPairs(const vector<int> &nums1, const vector<int> &nums2) : arr(nums2) { 6 for (const int n : nums1) 7 count1[n]++; 8 for (const int n : nums2) 9 count2[n]++; 10 } 11 12 void add(int index, int val) { 13 count2[arr[index]]--; 14 arr[index] += val; 15 count2[arr[index]]++; 16 } 17 18 int count(int tot) const { 19 int res = 0; 20 21 for (const auto [k, v] : count1) { 22 const auto it = count2.find(tot - k); 23 if (!v || it == count2.end()) continue; 24 res += v * it->second; 25 } 26 27 return res; 28 } 29 };