力扣(LeetCode)1801. 积压订单中的订单总数(C++)

优先队列+模拟

根据题目描述模拟。
如果该订单是一笔采购订单 buy ,则可以查看积压订单中价格 最低 的销售订单 sell 。提示我们,建立小根堆,维护价格最低的销售订单sell
反之亦然,如果该订单是一笔销售订单 sell ,则可以查看积压订单中价格 最高 的采购订单 buy 。提示我们,建立大根堆,维护价格最高的采购订单 buy

为了使答案返回积压订单数量,我们让优先队列保存元素类型pair<int,int>,同时记录{价格,数量}

遍历 order ,记录类型、价格、数量。根据类型操作即可。操作流程请看题目描述。

提示
  1. buysell 操作具有对称性,写好一个操作,另一个操作就好写了,照着逻辑改改。
  2. 本题重在模拟和容器使用,模拟是思路,容器使用是代码模板。
题目描述

pp
pp
pp

核心代码
class Solution {
public:
    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> qsell;
        priority_queue<pair<int,int>> qbuy;
        for(auto &x:orders){
            int type = x[2],amount = x[1], price = x[0];
            if(0==type){
                while(qsell.size()&&price>=qsell.top().first&&amount) {
                    if(amount>=qsell.top().second) {
                        amount -= qsell.top().second;
                        qsell.pop();
                    }else {
                        auto t = qsell.top();
                        t.second -= amount;
                        amount = 0;
                        qsell.pop();
                        qsell.push(t);
                    }
                }
                if(amount) qbuy.push({price,amount});
            }else{
                while(qbuy.size()&&price<=qbuy.top().first&&amount) {
                    if(amount>=qbuy.top().second) {
                        amount -= qbuy.top().second;
                        qbuy.pop();
                    }else {
                        auto t = qbuy.top();
                        t.second -= amount;
                        amount = 0;
                        qbuy.pop();
                        qbuy.push(t);
                    }
                }
                if(amount) qsell.push({price,amount});
            }
        }
        int ans = 0 ;
        const int mod = 1e9 + 7;
        while(qsell.size()) ans = (ans + qsell.top().second)%mod , qsell.pop();
        while(qbuy.size()) ans = (ans + qbuy.top().second)%mod , qbuy.pop();
        return ans;
    }
};
  1. 时间复杂度 : O ( n l o g n ) O(nlogn) O(nlogn) n n n 是订单总数,遍历订单,将订单加入优先队列的时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)
  2. 空间复杂度 : O ( n ) O(n) O(n) , 优先队列的空间复杂度 O ( n ) O(n) O(n)
AC

AC

致语
  • 理解思路很重要!
  • 欢迎读者在评论区留言,墨染看到就会回复的。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清墨韵染

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值