You are given two integer arrays nums1
and nums2
. You are tasked to implement a data structure that supports queries of two types:
- Add a positive integer to an element of a given index in the array
nums2
. - Count the number of pairs
(i, j)
such thatnums1[i] + nums2[j]
equals a given value (0 <= i < nums1.length
and0 <= j < nums2.length
).
Implement the FindSumPairs
class:
FindSumPairs(int[] nums1, int[] nums2)
Initializes theFindSumPairs
object with two integer arraysnums1
andnums2
.void add(int index, int val)
Addsval
tonums2[index]
, i.e., applynums2[index] += val
.int count(int tot)
Returns the number of pairs(i, j)
such thatnums1[i] + nums2[j] == tot
.
Example 1:
Input ["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]] Output [null, 8, null, 2, 1, null, null, 11] Explanation FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]); findSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4 findSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4] findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5 findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1 findSumPairs.add(0, 1); // now nums2 = [2,4,5,4,5,4] findSumPairs.add(1, 1); // now nums2 = [2,5,5,4,5,4] findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4
Constraints:
1 <= nums1.length <= 1000
1 <= nums2.length <= 105
1 <= nums1[i] <= 109
1 <= nums2[i] <= 105
0 <= index < nums2.length
1 <= val <= 105
1 <= tot <= 109
- At most
1000
calls are made toadd
andcount
each.
Solution: HashTable
Note nums1 and nums2 are unbalanced. Brute force method will take O(m*n) = O(103*105) = O(108) for each count call which will TLE. We could use a hashtable to store the counts of elements from nums2, and only iterate over nums1 to reduce the time complexity.
Time complexity:
init: O(m) + O(n)
add: O(1)
count: O(m)
Total time is less than O(106)
Space complexity: O(m + n)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Author: Huahua class FindSumPairs { public: FindSumPairs(vector<int>& nums1, vector<int>& nums2): nums1_(nums1), nums2_(nums2) { for (int x : nums2_) ++freq_[x]; } void add(int index, int val) { if (--freq_[nums2_[index]] == 0) freq_.erase(nums2_[index]); ++freq_[nums2_[index] += val]; } int count(int tot) { int ans = 0; for (int a : nums1_) { auto it = freq_.find(tot - a); if (it != freq_.end()) ans += it->second; } return ans; } private: vector<int> nums1_; vector<int> nums2_; unordered_map<int, int> freq_; }; |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Author: Huahua class FindSumPairs: def __init__(self, nums1: List[int], nums2: List[int]): self.nums1 = nums1 self.nums2 = nums2 self.freq = Counter(nums2) def add(self, index: int, val: int) -> None: self.freq.subtract((self.nums2[index],)) self.nums2[index] += val self.freq.update((self.nums2[index],)) def count(self, tot: int) -> int: return sum(self.freq[tot - a] for a in self.nums1) |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment