Number of Pairs After Increment Problem
Description
LeetCode Problem 3943.
You are given two integer arrays nums1 and nums2, and a 2D integer array queries.
Each queries[i] is one of the following types:
[1, x, y, val]– Addvalto every element innums2[x..y].[2, tot]– Compute the number of pairs(j, k)such thatnums1[j] + nums2[k] == tot.
Return an integer array answer, where answer[j] is the number of pairs for the j^th query of type 2.
Example 1:
Input: nums1 = [1,2], nums2 = [3,4], queries = [[2,5],[1,0,0,2],[2,5]]
Output: [2,1]
Explanation:
- queries[0] = [2, 5]: Valid pairs are nums1[0] + nums2[1] = 1 + 4 = 5 and nums1[1] + nums2[0] = 2 + 3 = 5.
- queries[1] = [1, 0, 0, 2]: Add 2 to nums2[0], resulting in nums2 = [5, 4].
- queries[2] = [2, 5]: Valid pair is nums1[0] + nums2[1] = 1 + 4 = 5.
- Thus, the answer = [2, 1].
Example 2:
Input: nums1 = [1,1], nums2 = [2,2,3], queries = [[2,4],[1,0,1,1],[2,4]]
Output: [2,6]
Explanation:
- queries[0] = [2, 4]: Valid pairs are nums1[0] + nums2[2] = 1 + 3 and nums1[1] + nums2[2] = 1 + 3.
- queries[1] = [1, 0, 1, 1]: Add 1 to nums2[0] and nums2[1], resulting in nums2 = [3, 3, 3].
- queries[2] = [2, 4]: Every element of nums1 = [1, 1] pairs with every element of nums2 = [3, 3, 3] as 1 + 3 = 4. That gives 2 × 3 = 6 pairs in total.
- Thus, the answer = [2, 6].
Example 3:
Input: nums1 = [2,5,8,4], nums2 = [1,3,8], queries = [[2,9],[1,1,2,1],[2,10]]
Output: [1,0]
Explanation:
- queries[0] = [2, 9]: Only valid pair is nums1[2] + nums2[0] = 8 + 1 = 9.
- queries[1] = [1, 1, 2, 1]: Add 1 to nums2[1] and nums2[2], resulting in nums2 = [1, 4, 9].
- queries[2] = [2, 10]: No pair sums to 10.
- Thus, the answer = [1, 0].
Constraints:
1 <= nums1.length <= 51 <= nums2.length <= 5 * 10^41 <= nums1[i], nums2[i] <= 10^51 <= queries.length <= 5 * 10^4queries[i].length == 2 or 4queries[i] == [1, x, y, val], orqueries[i] == [2, tot]0 <= x <= y < nums2.length1 <= val <= 10^51 <= tot <= 10^9
Solution
Approach: Sqrt Decomposition with Hash Maps
nums1 has at most 5 elements, so a type 2 query only has to count, for each x in nums1, how many elements of nums2 equal tot - x. The hard part is the range additions.
Split nums2 into blocks of about sqrt(n) elements. For each block keep:
- a hash map from stored value to its count, and
- a lazy value added to the whole block, so the real value of an element is its stored value plus the lazy value of its block.
For an update [1, x, y, val], blocks that are fully covered just add val to their lazy value. The two partial blocks are updated element by element, moving each element's count in the hash map. For a query [2, tot], we visit every block and look up tot - x - lazy for each x in nums1, then add up the counts. Use 64-bit integers, because values can grow large.
Complexity: each update costs O(sqrt(n)) and each query costs O(5 sqrt(n)) hash lookups, so the total is O(q sqrt(n)) time and O(n) space.
class Solution:
def numberOfPairs(
self, nums1: List[int], nums2: List[int], queries: List[List[int]]
) -> List[int]:
n = len(nums2)
size = max(1, int(n**0.5))
blocks = (n + size - 1) // size
a = nums2[:] # a[i] + lazy[i // size] is the real value
lazy = [0] * blocks
cnt = [dict() for _ in range(blocks)]
for i, v in enumerate(a):
d = cnt[i // size]
d[v] = d.get(v, 0) + 1
def bump(i: int, val: int) -> None:
d = cnt[i // size]
d[a[i]] -= 1
a[i] += val
d[a[i]] = d.get(a[i], 0) + 1
ans = []
for q in queries:
if q[0] == 1:
_, x, y, val = q
bx, by = x // size, y // size
if bx == by:
for i in range(x, y + 1):
bump(i, val)
else:
for i in range(x, (bx + 1) * size):
bump(i, val)
for b in range(bx + 1, by):
lazy[b] += val
for i in range(by * size, y + 1):
bump(i, val)
else:
tot = q[1]
res = 0
for b in range(blocks):
d = cnt[b]
lz = lazy[b]
for x in nums1:
res += d.get(tot - x - lz, 0)
ans.append(res)
return ans