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)


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