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 queryxi = 4
andyi = 1
, we can select indexj = 0
sincenums1[j] >= 4
andnums2[j] >= 1
. The sumnums1[j] + nums2[j]
is 6, and we can show that 6 is the maximum we can obtain. For the 2nd queryxi = 1
andyi = 3
, we can select indexj = 2
sincenums1[j] >= 1
andnums2[j] >= 3
. The sumnums1[j] + nums2[j]
is 10, and we can show that 10 is the maximum we can obtain. For the 3rd queryxi = 2
andyi = 5
, we can select indexj = 3
sincenums1[j] >= 2
andnums2[j] >= 5
. The sumnums1[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 withxi
= 3 andyi
= 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
class Solution {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
int n = nums1.length, m = q.length;
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = nums1[i];
a[i][1] = nums2[i];
}
int[][] b = new int[m][3];
for (int i = 0; i < m; i++) {
b[i][0] = q[i][0];
b[i][1] = q[i][1];
b[i][2] = i;
}
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
TreeMap<Integer, Integer> map = new TreeMap<>();
int[] res = new int[m];
int max = -1;
for (int i = m - 1, j = n - 1; i >= 0; i--) {
int x = b[i][0], y = b[i][1], idx = b[i][2];
while (j >= 0 && a[j][0] >= x) {
if (max < a[j][1]) {
max = a[j][1];
Integer key = map.floorKey(a[j][1]);
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
map.remove(key);
key = map.floorKey(key);
}
map.put(max, a[j][0] + a[j][1]);
}
j--;
}
Integer key = map.ceilingKey(y);
if (key == null)
res[idx] = -1;
else
res[idx] = map.get(key);
}
return res;
}
}