【Leetcode】1801. Number of Orders in the Backlog

文章提供了一个在LeetCode上的问题解决方案,涉及股票订单的处理。通过使用两个优先级队列(一个用于存储买单,一个用于存储卖单),按价格优先撮合交易,最后计算未完成交易的股数。算法的时间复杂度为O(nlogn),空间复杂度为O(n)。
摘要由CSDN通过智能技术生成

题目地址:

https://leetcode.com/problems/number-of-orders-in-the-backlog/description/

给定若干的下单,每个下单包含价格、股数和下单是买还是卖, 1 1 1是买, 0 0 0是卖。每个单下了以后,如果其是买单,则其会按照价格优先的方式满足价格低于或等于买单价格,然后撮合交易。如果是卖单则类似。问最后还有多少股数没有被交易完。答案模 1 0 9 + 7 10^9+7 109+7后返回。

可以开两个堆,分别存买单和卖单,按价格优先排序,买单的堆顶是最大买价,卖单的堆顶是最小卖价,这样一旦来了一个单子,就可以看堆顶是否能撮合交易。代码如下:

class Solution {
 public:
  using PII = pair<int, int>;
#define x first
#define y second
  int getNumberOfBacklogOrders(vector<vector<int>> &orders) {
    static const int MOD = 1e9 + 7;
    priority_queue<PII> buys;
    priority_queue<PII, vector<PII>, greater<>> sells;
    for (auto &o : orders) {
      if (!o[2]) {
        while (sells.size() && sells.top().x <= o[0]) {
          auto p = sells.top();
          sells.pop();
          int cnt = min(p.y, o[1]);
          p.y -= cnt;
          o[1] -= cnt;
          if (p.y) {
            sells.push(p);
            break;
          }
        }
        if (!o[1]) continue;
        if (buys.size() && buys.top().x == o[0]) {
          auto p = buys.top();
          buys.pop();
          p.y += o[1];
          buys.push(p);
        } else
          buys.emplace(o[0], o[1]);
      } else {
        while (buys.size() && buys.top().x >= o[0]) {
          auto p = buys.top();
          buys.pop();
          int cnt = min(p.y, o[1]);
          p.y -= cnt;
          o[1] -= cnt;
          if (p.y) {
            buys.push(p);
            break;
          }
        }
        if (!o[1]) continue;
        if (sells.size() && sells.top().x == o[0]) {
          auto p = sells.top();
          sells.pop();
          p.y += o[1];
          sells.push(p);
        } else
          sells.emplace(o[0], o[1]);
      }
    }

    int res = 0;
    for (; buys.size(); buys.pop()) res = (res + buys.top().y) % MOD;
    for (; sells.size(); sells.pop()) res = (res + sells.top().y) % MOD;
    return res;
  }
};

时间复杂度 O ( n log ⁡ n ) O(n\log n) O(nlogn),空间 O ( n ) O(n) O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值