【每日一题Day75】LC1801积压订单中的订单总数 | 优先队列

文章介绍了一个处理积压订单的问题,其中涉及买卖订单的匹配和执行。通过使用两个优先队列,一个存储buy订单,另一个存储sell订单,根据价格进行排序,依次处理订单。在遍历所有输入订单后,返回积压订单的总数,对大整数结果取模运算以防止溢出。时间复杂度为O(nlogn),空间复杂度为O(n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

积压订单中的订单总数【LC1801】

You are given a 2D integer array orders, where each orders[i] = [pricei, amounti, orderTypei] denotes that amounti orders have been placed of type orderTypei at the price pricei. The orderTypei is:

  • 0 if it is a batch of buy orders, or
  • 1 if it is a batch of sell orders.

Note that orders[i] represents a batch of amounti independent orders with the same price and order type. All orders represented by orders[i] will be placed before all orders represented by orders[i+1] for all valid i.

There is a backlog that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:

  • If the order is a buy order, you look at the sell order with the smallest price in the backlog. If that sell order’s price is smaller than or equal to the current buy order’s price, they will match and be executed, and that sell order will be removed from the backlog. Else, the buy order is added to the backlog.
  • Vice versa, if the order is a sell order, you look at the buy order with the largest price in the backlog. If that buy order’s price is larger than or equal to the current sell order’s price, they will match and be executed, and that buy order will be removed from the backlog. Else, the sell order is added to the backlog.

Return the total amount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo 109 + 7.

  • 思路:使用两个优先队列存储类型为buy或者sell的积压订单,根据规则删除积压订单,并统计积压订单的数量,最后返回结果即可

    • 当遍历到的订单是采购订单时,从优先队列sell中删除能够删除的所有积压订单,销售价格需小于等于采购价格,并且需要优先删除销售价格较小的订单,因此优先队列sellprice升序排序
    • 当遍历到的订单是销售订单时,从优先队列buy中删除能够删除的所有积压订单,采购价格需大于等于销售价格,并且需要优先删除采购价格更大的订单,因此优先队列buyprice降序排序
  • 实现

    • 使用int类型变量count统计积压订单的数量,遇到新订单时先将新订单数量累加至count,每次提交订单时删除相应的数量,注意减法为了避免负数取余时需要加MOD
    class Solution {
        public int getNumberOfBacklogOrders(int[][] orders) {
            int MOD = (int)(1e9 + 7);
            PriorityQueue<int[]> buy = new PriorityQueue<int[]>((o1, o2) -> (o2[0] - o1[0]));
            PriorityQueue<int[]> sell = new PriorityQueue<int[]>((o1, o2) -> (o1[0] - o2[0]));        
            long count = 0;// 统计积压订单数量
            for (int[] order : orders){
                int price = order[0], amount = order[1], type = order[2];
                count = (count + amount) % MOD;
                if (type == 0){// buy                
                    while(!sell.isEmpty() && sell.peek()[0] <= price){
                        int[] minSell = sell.poll();
                        int num = Math.min(amount, minSell[1]);
                        minSell[1] -= num;
                        amount -= num;
                        // count = (count - 2 * num) % MOD;
                        count = (count + MOD- 2 * num) % MOD;// 避免遇到负数
                        if (amount == 0 && minSell[1] != 0){
                            sell.add(new int[]{minSell[0], minSell[1]});
                            break;
                        }
                    }
                    if (amount != 0){
                        buy.add(new int[]{price, amount});
                    }
                }else{// sell
                    while(!buy.isEmpty() && buy.peek()[0] >= price){
                        int[] maxBuy = buy.poll();
                        int num = Math.min(amount, maxBuy[1]);
                        maxBuy[1] -= num;
                        amount -= num;
                        // count = (count - 2 * num) % MOD;
                        count = (count + MOD- 2 * num) % MOD;
                        if (amount == 0 && maxBuy[1] != 0){
                            buy.add(new int[]{maxBuy[0], maxBuy[1]});
                            break;
                        }
                    }
                    if (amount != 0){
                        sell.add(new int[]{price, amount});
                    }
                }
            }
            return (int)count;
        }
    }
    
    • 复杂度
      • 时间复杂度:O(nlogn)O(nlogn)O(nlogn),需要遍历数组 orders 一次,对于每个元素处理优先队列的时间是 O(logn)O(logn)O(logn),共需要 O(nlog⁡n)O(nlog⁡n)O(nlogn)的时间,遍历结束之后计算剩余的积压订单总数需要 O(nlog⁡n)O(nlog⁡n)O(nlogn) 的时间。
      • 空间复杂度:O(n)O(n)O(n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值