【论文阅读】2736. 最大和查询-2023.11.17

题目:

2736. 最大和查询

给你两个长度为 n 、下标从 0 开始的整数数组 nums1nums2 ,另给你一个下标从 1 开始的二维数组 queries ,其中 queries[i] = [xi, yi]

对于第 i 个查询,在所有满足 nums1[j] >= xinums2[j] >= yi 的下标 j (0 <= j < n) 中,找出 nums1[j] + nums2[j]最大值 ,如果不存在满足条件的 j 则返回 -1

返回数组answer 其中answer[i]是第 i 个查询的答案。

示例 1:

复制代码
输入:nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]
输出:[6,10,7]
解释:
对于第 1 个查询:xi = 4且 yi = 1,可以选择下标 j = 0 ,此时 nums1[j] >= 4且 nums2[j] >= 1。nums1[j] + nums2[j]等于 6 ,可以证明 6 是可以获得的最大值。
对于第 2 个查询:xi = 1 且 yi = 3 ,可以选择下标 j = 2,此时 nums1[j] >= 1且 nums2[j] >= 3。nums1[j] + nums2[j]等于 10 ,可以证明 10 是可以获得的最大值。
对于第 3 个查询:xi = 2且 yi = 5,可以选择下标 j = 3 ,此时 nums1[j] >= 2且 nums2[j] >= 5。nums1[j] + nums2[j]等于 7 ,可以证明 7 是可以获得的最大值。
因此,我们返回 [6,10,7]。

示例 2:

复制代码
输入:nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]
输出:[9,9,9]
解释:对于这个示例,我们可以选择下标 j = 2,该下标可以满足每个查询的限制。

示例 3:

复制代码
输入:nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]
输出:[-1]
解释:示例中的查询 xi = 3 且 yi= 3 。对于每个下标 j ,都只满足 nums1[j] < xi或者 nums2[j] <yi。因此,不存在答案。 

提示:

  • 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[][] queries) {
        int n=nums1.length;
        int[][] sortedNums=new int[n][2];
        for(int i=0;i<n;i++){
            sortedNums[i][0]=nums1[i];
            sortedNums[i][1]=nums2[i];
        }
        Arrays.sort(sortedNums,(a,b)->b[0]-a[0]);
        int q=queries.length;
        int[][] sortedQueries=new int[q][3];
        for(int i=0;i<q;i++){
            sortedQueries[i][0]=i;
            sortedQueries[i][1]=queries[i][0];
            sortedQueries[i][2]=queries[i][1];
        }
        Arrays.sort(sortedQueries,(a,b)->b[1]-a[1]);
        List<int[]> stack=new ArrayList<int[]>();
        int[] answer=new int[q];
        Arrays.fill(answer,-1);
        int j=0;
        for(int[] query:sortedQueries){
            int i=query[0],x=query[1],y=query[2];
            while(j<n&&sortedNums[j][0]>=x){
                int[] pair=sortedNums[j];
                int num1=pair[0];
                int num2=pair[1];
                while(!stack.isEmpty()&&stack.get(stack.size()-1)[1]<=num1+num2){
                    stack.remove(stack.size()-1);
                }
                if(stack.isEmpty()||stack.get(stack.size()-1)[0]<num2){
                    stack.add(new int[]{num2,num1+num2});
                }
                j++;
            }
            int k=binarySearch(stack,y);
            if(k<stack.size()){
                answer[i]=stack.get(k)[1];
            }
        }
        return answer;
    }
    public int binarySearch(List<int[]> list,int target){
        int low=0,high=list.size();
        while(low<high){
            int mid=low+(high-low)/2;
            if(list.get(mid)[0]>=target){
                high=mid;
            }else{
                low=mid+1;
            }
        }
        return low;
    }
}

结果:

相关推荐
yvestine几秒前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
慢半拍iii18 分钟前
数据结构——F/图
c语言·开发语言·数据结构·c++
GalaxyPokemon30 分钟前
LeetCode - 148. 排序链表
linux·算法·leetcode
iceslime1 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
aichitang20242 小时前
矩阵详解:从基础概念到实际应用
线性代数·算法·矩阵
OpenCSG2 小时前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
witton3 小时前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
chao_7893 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
dfsj660113 小时前
LLMs 系列科普文(14)
人工智能·深度学习·算法
薛定谔的算法4 小时前
《盗梦空间》与JavaScript中的递归
算法