Maximum Sum Queries Problem
Description
LeetCode Problem 2736.
You are given two 0-indexed integer arrays nums1 and nums2, each of length n, and a 1-indexed 2D array queries where queries[i] = [x_i, y_i].
For the i^th query, find the maximum value of nums1[j] + nums2[j] among all indices j (0 <= j < n), where nums1[j] >= x_i and nums2[j] >= y_i, or -1 if there is no j satisfying the constraints.
Return an array answer where answer[i] is the answer to the i^th query.
Example 1:
Input: nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]
Output: [6,10,7]
Explanation:
For the 1st query x_i = 4 and y_i = 1, we can select index j = 0 since nums1[j] >= 4 and nums2[j] >= 1. The sum nums1[j] + nums2[j] is 6, and we can show that 6 is the maximum we can obtain.
For the 2nd query x_i = 1 and y_i = 3, we can select index j = 2 since nums1[j] >= 1 and nums2[j] >= 3. The sum nums1[j] + nums2[j] is 10, and we can show that 10 is the maximum we can obtain.
For the 3rd query x_i = 2 and y_i = 5, we can select index j = 3 since nums1[j] >= 2 and nums2[j] >= 5. The sum nums1[j] + nums2[j] is 7, and we can show that 7 is the maximum we can obtain.
Therefore, we return [6,10,7].
Example 2:
Input: nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]
Output: [9,9,9]
Explanation: For this example, we can use index j = 2 for all the queries since it satisfies the constraints for each query.
Example 3:
Input: nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]
Output: [-1]
Explanation: There is one query in this example with x_i = 3 and y_i = 3. For every index, j, either nums1[j] < x_i or nums2[j] < y_i. Hence, there is no solution.
Constraints:
nums1.length == nums2.lengthn == nums1.length1 <= n <= 10^51 <= nums1[i], nums2[i] <= 10^91 <= queries.length <= 10^5queries[i].length == 2x_i == queries[i][1]y_i == queries[i][2]1 <= x_i, y_i <= 10^9
Solution
Approach: Offline Queries and a Binary Indexed Tree
This is a two-dimensional dominance problem: for each query (x, y) we want the largest nums1[j] + nums2[j] among points with nums1[j] >= x and nums2[j] >= y. The usual trick is to sort by one coordinate and handle the other with a data structure.
- Sort the points by
nums1in descending order, and sort the query indices byxin descending order. - Process the queries in that order. Before answering a query, insert every point with
nums1 >= xthat has not been inserted yet (a pointer only moves forward). - Compress the
nums2values by their rank in the sortednums2. A Fenwick tree (binary indexed tree) stores, at the rank of each inserted point, the maximum ofnums1 + nums2. We want a maximum over all ranks at least as large as the rank ofy, so we index the tree in reverse (n - rank) and turn that into a prefix-maximum query. - The answer to the query is that prefix maximum, or
-1when nothing has been inserted in the range.
Complexity: O((n + m) log n + m log m) time and O(n + m) space, for n points and m queries.
from bisect import bisect_left
class BinaryIndexedTree:
__slots__ = ["n", "c"]
def __init__(self, n: int):
self.n = n
self.c = [-1] * (n + 1)
def update(self, x: int, v: int):
while x <= self.n:
self.c[x] = max(self.c[x], v)
x += x & -x
def query(self, x: int) -> int:
mx = -1
while x:
mx = max(mx, self.c[x])
x -= x & -x
return mx
class Solution:
def maximumSumQueries(
self, nums1: List[int], nums2: List[int], queries: List[List[int]]
) -> List[int]:
nums = sorted(zip(nums1, nums2), key=lambda x: -x[0])
nums2.sort()
n, m = len(nums1), len(queries)
ans = [-1] * m
j = 0
tree = BinaryIndexedTree(n)
for i in sorted(range(m), key=lambda i: -queries[i][0]):
x, y = queries[i]
while j < n and nums[j][0] >= x:
k = n - bisect_left(nums2, nums[j][1])
tree.update(k, nums[j][0] + nums[j][1])
j += 1
k = n - bisect_left(nums2, y)
ans[i] = tree.query(k)
return ans