g2701_2800.s2736_maximum_sum_queries.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2701_2800.s2736_maximum_sum_queries;
// #Hard #Array #Sorting #Binary_Search #Stack #Monotonic_Stack #Segment_Tree #Binary_Indexed_Tree
// #2023_09_23_Time_66_ms_(78.43%)_Space_84.1_MB_(94.12%)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* 2736 - Maximum Sum Queries\.
*
* Hard
*
* 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
**/
public class Solution {
private void update(NavigableMap map, int num, int sum) {
Map.Entry entry = map.floorEntry(num);
while (entry != null && entry.getValue() <= sum) {
map.remove(entry.getKey());
int x = entry.getKey();
entry = map.floorEntry(x);
}
entry = map.ceilingEntry(num);
if (entry == null || entry.getValue() < sum) {
map.put(num, sum);
}
}
private int queryVal(NavigableMap map, int num) {
Map.Entry entry = map.ceilingEntry(num);
if (entry == null) {
return -1;
}
return entry.getValue();
}
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) {
int n = nums1.length;
int m = queries.length;
List v = new ArrayList<>();
for (int i = 0; i < n; i++) {
v.add(new int[] {nums1[i], nums2[i]});
}
v.sort(Comparator.comparingInt(a -> a[0]));
List ind = new ArrayList<>();
for (int i = 0; i < m; i++) {
ind.add(i);
}
ind.sort((a, b) -> queries[b][0] - queries[a][0]);
TreeMap values = new TreeMap<>();
int j = n - 1;
int[] ans = new int[m];
for (int i : ind) {
int a = queries[i][0];
int b = queries[i][1];
for (; j >= 0 && v.get(j)[0] >= a; j--) {
update(values, v.get(j)[1], v.get(j)[0] + v.get(j)[1]);
}
ans[i] = queryVal(values, b);
}
return ans;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy