LC 1865 [M] Finding Pairs With a Certain Sum - ALawliet/algorithms GitHub Wiki
2Sum hashmap
class FindSumPairs:
def __init__(self, nums1: List[int], nums2: List[int]):
self.nums1 = nums1
self.nums2 = nums2
self.freq2 = Counter(nums2)
def add(self, index: int, val: int) -> None:
self.freq2[self.nums2[index]] -= 1 # Remove old one
self.nums2[index] += val
self.freq2[self.nums2[index]] += 1 # Count new one
def count(self, tot: int) -> int:
count = 0
for a in self.nums1:
addend = tot - a
count += self.freq2[addend] # a + b = tot -> b = tot - a
return count
# Your FindSumPairs object will be instantiated and called as such:
# obj = FindSumPairs(nums1, nums2)
# obj.add(index,val)
# param_2 = obj.count(tot)