LeetCode 2736. Maximum Sum Queries Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2736. Maximum Sum Queries

Description

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] = [xi, yi].

For the ith query, find the maximum value of nums1[j] + nums2[j] among all indices j (0 <= j < n), where nums1[j] >= xi and nums2[j] >= yi, or -1 if there is no j satisfying the constraints.

Return an array answer where answer[i] is the answer to the ith 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 xi = 4 and yi = 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 xi = 1 and yi = 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 xi = 2 and yi = 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 xi = 3 and yi = 3. For every index, j, either nums1[j] < xi or nums2[j] < yi. Hence, there is no solution. 

 

Constraints:

  • nums1.length == nums2.length 
  • n == nums1.length 
  • 1 <= n <= 105
  • 1 <= nums1[i], nums2[i] <= 109 
  • 1 <= queries.length <= 105
  • queries[i].length == 2
  • xi == queries[i][1]
  • yi == queries[i][2]
  • 1 <= xi, yi <= 109

Solutions

Solution 1: Binary Indexed Tree

This problem belongs to the category of two-dimensional partial order problems.

A two-dimensional partial order problem is defined as follows: given several pairs of points (a1, b1), (a2, b2), ..., (an, bn), and a defined partial order relation, now given a point (ai, bi), we need to find the number/maximum value of point pairs (aj, bj) that satisfy the partial order relation. That is:

(aj, bj) \prec(ai, bi) \stackrel{\text { def }}{=} aj \lesseqgtr ai \text { and } bj \lesseqgtr bi

The general solution to two-dimensional partial order problems is to sort one dimension and use a data structure to handle the second dimension (this data structure is generally a binary indexed tree).

For this problem, we can create an array nums, where nums[i]=(nums1[i], nums2[i]), and then sort nums in descending order according to nums1. We also sort the queries queries in descending order according to x.

Next, we iterate through each query queries[i] = (x, y). For the current query, we loop to insert the value of nums2 for all elements in nums that are greater than or equal to x into the binary indexed tree. The binary indexed tree maintains the maximum value of nums1 + nums2 in the discretized nums2 interval. Therefore, we only need to query the maximum value corresponding to the interval greater than or equal to the discretized y in the binary indexed tree. Note that since the binary indexed tree maintains the prefix maximum value, we can insert nums2 in reverse order into the binary indexed tree in the implementation.

The time complexity is O((n + m) × log n + m × log m), and the space complexity is O(n + m). Here, n is the length of the array nums, and m is the length of the array queries.

Similar problems:

PythonJavaC++GoTypeScript
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(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !