Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

    • 搜索

    • 二分查找

    • 排序

      • 215.数组中的第K个最大元素(堆排,快排)
      • 88.合并两个有序数组
      • 179.最大数
      • 912. 归并排序
      • 1090. 受标签影响的最大值
      • 1439. 有序矩阵中的第 k 个最小数组和
      • 347. 前 K 个高频元素
      • 621. 任务调度器
      • 406. 根据身高重建队列
      • 2475. 数组中不等三元组的数目
      • 31. 下一个排列
      • 6929. 数组的最大美丽值
      • 2736. 最大和查询
        • 1.二分+单调栈
      • 剑指 Offer 40. 最小的k个数
      • 剑指 Offer 45. 把数组排成最小的数
      • 剑指 Offer 51. 数组中的逆序对
    • 边界判断

    • 双指针法

    • 连续子数组

    • 差分数组

    • 模拟

    • 区间问题

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 数组
  • 排序
phan
2023-11-17
目录

2736. 最大和查询

# 2736. 最大和查询 (opens new window)

# 1.二分+单调栈

将nums1和nums2合并,并进行预处理。预处理分为两个步骤:

  • 将数组按照nums1元素降序排序
  • 将查询数组queries按照第一个元素(对应nums1限制条件)降序排序。

对于每个查询,将nums当中符合nums1约束的数对加入单调栈当中,而前面排过序,保证单调栈的数对中nums1是递减的。然后再对栈进行二分,找到满足nums2约束的数对。

对于每次查询,假设前一个元素是a,b,下一个满足nums1约束的数对是c,d。因为前面预处理所以有a>c,那么数对(c,d)的入栈情况可以分为以下几种情况讨论:

  • 如果当前b>=d,那么后续查询当中(a,b)比(c,d)更有资格成为最大和查询的结果。因此入栈的元素一定满足nums2[y]<nums2[y'],即自栈底到栈顶nums2是递增的。
  • 排除掉栈中的无效元素:如果c+d>a+b,那么后续的查询当中,(c,d)数对更有资格成为最大和查询,因为查询也是按照queries[0]降序排序的,因此如果有c>=x,那么后续查询中也一定有c>=x'。因此入栈前,只要栈顶数对和小于当前数对和则需要出栈,维护单调栈中数对和是递减的。

最终,维护的单调栈自底至栈顶,nums1递减,nums2递增,nums1+nums2递减。最后使用二分找到最小满足nums2<y的数对,即为最大和。

💡启发:多维数组的单调约束问题,不一定需要同时对多个维度进行排序,可以先对其中一个维度排序入手,再使用数据结构对元素进行筛选过滤。

class Solution {
    public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) {
        int[] res = new int[queries.length];
        int[][] nums = new int[nums1.length][2];
        for (int i = 0; i < nums1.length; i++) {
            nums[i][0] = nums1[i];
            nums[i][1] = nums2[i];
        }
        Arrays.sort(nums, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                return o2[0] - o1[0];
            }
        });

        Integer[] queriesInx = new Integer[queries.length];
        for (int i = 0; i < queries.length; i++) {
            queriesInx[i] = i;
        }
        Arrays.sort(queriesInx, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return queries[o2][0] - queries[o1][0];
            }
        });
        
        List<int[]> stack = new ArrayList<>();
        int nums1Inx=0;
        for (int i = 0; i < queries.length; i++) {
            int x = queries[queriesInx[i]][0];
            int y = queries[queriesInx[i]][1];
            //找出满足nums1>=x的元素
            for (; nums1Inx < nums1.length && nums[nums1Inx][0] >= x; nums1Inx++) {
                int tmpx = nums[nums1Inx][0];
                int tmpy = nums[nums1Inx][1];
                //维护单调栈元素:自栈底到栈顶依次是:nums1递减,nums2递增,(nums1+nums2)递减
                while (!stack.isEmpty()) {
                    int[] tail = stack.get(stack.size() - 1);
                    if(tail[0]+tail[1]<=tmpx+tmpy) stack.remove(stack.size() - 1);
                    else break;
                }
                 if(stack.isEmpty()||stack.get(stack.size()-1)[1]<tmpy) stack.add(nums[nums1Inx]);
            }
            //二分
            int ansInx = stack.size()==0?-1:lowhigh(stack, y);
            res[queriesInx[i]] = ansInx == -1 ? -1 : stack.get(ansInx)[0] + stack.get(ansInx)[1];

        }
        return res;
    }

    public int lowhigh(List<int[]> stack, int y) {
        int low = 0, high = stack.size() - 1;
        while (low < high) {
            int mid = (low + high) >> 1;
            if(stack.get(mid)[1]<y) low = mid + 1;
            else high = mid;
        }
        return stack.get(low)[1]>=y?low:-1;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
编辑 (opens new window)
#Leetcode#排序
上次更新: 2023/12/15, 15:49:57
6929. 数组的最大美丽值
剑指 Offer 40. 最小的k个数

← 6929. 数组的最大美丽值 剑指 Offer 40. 最小的k个数→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式