1865. Finding Pairs With a Certain Sum
https://leetcode.com/problems/finding-pairs-with-a-certain-sum
Description
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:
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.
ac
Last updated