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

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

积压订单中的订单总数【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中删除能够删除的所有积压订单,销售价格需小于等于采购价格,并且需要优先删除销售价格较小的订单,因此优先队列sell以price升序排序


。当遍历到的订单是销售订单时,从优先队列buy中删除能够删除的所有积压订单,采购价格需大于等于销售价格,并且需要优先删除采购价格更大的订单,因此优先队列buy以price降序排序


  • 实现


。使用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),需要遍历数组 orders 一次,对于每个元素处理优先队列的时间是O(logn),共需要 O(nlog⁡n)的时间,遍历结束之后计算剩余的积压订单总数需要O(nlog⁡n) 的时间。
  • 空间复杂度:O ( n )
目录
相关文章
|
8月前
【Leetcode 1944】队列中可以看到的人数 —— 单调栈
解题思路:维持一个单调递增栈来辅助计算每个人能够看到的人数。从右往左遍历数组,对于每个人,我们将其身高与栈顶元素的身高进行比较。如果当前人的身高比栈顶元素的身高高,则栈顶元素无法被当前人看到,将其出栈,并累计计数
|
7月前
|
Java Python
二刷力扣--链表
二刷力扣--链表
|
7月前
|
存储 人工智能 测试技术
每日练习之排序——链表的合并;完全背包—— 兑换零钱
每日练习之排序——链表的合并;完全背包—— 兑换零钱
43 2
|
8月前
|
算法 测试技术 C#
【最大值线段树】【二分查找】2286. 以组为单位订音乐会的门票
【最大值线段树】【二分查找】2286. 以组为单位订音乐会的门票
|
8月前
|
存储
leetcode1944. 队列中可以看到的人数
leetcode1944. 队列中可以看到的人数
39 0
|
8月前
|
存储 搜索推荐 算法
14.如何把百万级别订单根据金额排序
14.如何把百万级别订单根据金额排序
46 0
|
8月前
|
存储 搜索推荐 算法
如何把百万级别的订单根据金额排序
如何把百万级别的订单根据金额排序
53 0
|
8月前
|
算法 测试技术 C#
【单调栈】LeetCode:1944队列中可以看到的人数
【单调栈】LeetCode:1944队列中可以看到的人数
淘宝批量复制宝贝提示“当前类目大于48小时发货的发货时间不能大于15天,请调整”怎么解决?
要复制这个宝贝上传到淘宝店铺,只需要重新复制一次,然后在大淘营淘宝宝贝复制专家下载配置的第二步,选择一个小于或等于15天的发货时间(见下图),这样就可以复制宝贝上传到淘宝店铺了。
|
SQL Java 数据库
Java开发篇 - 库存超卖,库存扣成负数?
库存扣减的时机处理,有对与不对。
306 0

热门文章

最新文章

下一篇
开通oss服务