#1801
Medium
auto_awesome

LeetCode 题解工作台

积压订单中的订单总数

给你一个二维整数数组 orders ,其中每个 orders[i] = [price i , amount i , orderType i ] 表示有 amount i 笔类型为 orderType i 、价格为 price i 的订单。 订单类型 orderType i 可以分为两种: 0 表示这…

category

3

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 ·

bolt

答案摘要

我们可以使用优先队列(大小根堆)维护当前的积压订单,其中大根堆 `buy` 维护积压的采购订单,小根堆 `sell` 维护积压的销售订单。堆中每个元素是一个二元组 $(price, amount)$,表示价格为 `price` 的订单数量为 `amount`。 接下来,我们遍历订单数组 `orders` ,根据题意模拟即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 堆 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二维整数数组 orders ,其中每个 orders[i] = [pricei, amounti, orderTypei] 表示有 amounti 笔类型为 orderTypei 、价格为 pricei 的订单。

订单类型 orderTypei 可以分为两种:

  • 0 表示这是一批采购订单 buy
  • 1 表示这是一批销售订单 sell

注意,orders[i] 表示一批共计 amounti 笔的独立订单,这些订单的价格和类型相同。对于所有有效的 i ,由 orders[i] 表示的所有订单提交时间均早于 orders[i+1] 表示的所有订单。

存在由未执行订单组成的 积压订单 。积压订单最初是空的。提交订单时,会发生以下情况:

  • 如果该订单是一笔采购订单 buy ,则可以查看积压订单中价格 最低 的销售订单 sell 。如果该销售订单 sell 的价格 低于或等于 当前采购订单 buy 的价格,则匹配并执行这两笔订单,并将销售订单 sell 从积压订单中删除。否则,采购订单 buy 将会添加到积压订单中。
  • 反之亦然,如果该订单是一笔销售订单 sell ,则可以查看积压订单中价格 最高 的采购订单 buy 。如果该采购订单 buy 的价格 高于或等于 当前销售订单 sell 的价格,则匹配并执行这两笔订单,并将采购订单 buy 从积压订单中删除。否则,销售订单 sell 将会添加到积压订单中。

输入所有订单后,返回积压订单中的 订单总数 。由于数字可能很大,所以需要返回对 109 + 7 取余的结果。

 

示例 1:

输入:orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
输出:6
解释:输入订单后会发生下述情况:
- 提交 5 笔采购订单,价格为 10 。没有销售订单,所以这 5 笔订单添加到积压订单中。
- 提交 2 笔销售订单,价格为 15 。没有采购订单的价格大于或等于 15 ,所以这 2 笔订单添加到积压订单中。
- 提交 1 笔销售订单,价格为 25 。没有采购订单的价格大于或等于 25 ,所以这 1 笔订单添加到积压订单中。
- 提交 4 笔采购订单,价格为 30 。前 2 笔采购订单与价格最低(价格为 15)的 2 笔销售订单匹配,从积压订单中删除这 2 笔销售订单。第 3 笔采购订单与价格最低的 1 笔销售订单匹配,销售订单价格为 25 ,从积压订单中删除这 1 笔销售订单。积压订单中不存在更多销售订单,所以第 4 笔采购订单需要添加到积压订单中。
最终,积压订单中有 5 笔价格为 10 的采购订单,和 1 笔价格为 30 的采购订单。所以积压订单中的订单总数为 6 。

示例 2:

输入:orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
输出:999999984
解释:输入订单后会发生下述情况:
- 提交 109 笔销售订单,价格为 7 。没有采购订单,所以这 109 笔订单添加到积压订单中。
- 提交 3 笔采购订单,价格为 15 。这些采购订单与价格最低(价格为 7 )的 3 笔销售订单匹配,从积压订单中删除这 3 笔销售订单。
- 提交 999999995 笔采购订单,价格为 5 。销售订单的最低价为 7 ,所以这 999999995 笔订单添加到积压订单中。
- 提交 1 笔销售订单,价格为 5 。这笔销售订单与价格最高(价格为 5 )的 1 笔采购订单匹配,从积压订单中删除这 1 笔采购订单。
最终,积压订单中有 (1000000000-3) 笔价格为 7 的销售订单,和 (999999995-1) 笔价格为 5 的采购订单。所以积压订单中的订单总数为 1999999991 ,等于 999999984 % (109 + 7) 。

 

提示:

  • 1 <= orders.length <= 105
  • orders[i].length == 3
  • 1 <= pricei, amounti <= 109
  • orderTypei01
lightbulb

解题思路

方法一:优先队列(大小根堆) + 模拟

我们可以使用优先队列(大小根堆)维护当前的积压订单,其中大根堆 buy 维护积压的采购订单,小根堆 sell 维护积压的销售订单。堆中每个元素是一个二元组 (price,amount)(price, amount),表示价格为 price 的订单数量为 amount

接下来,我们遍历订单数组 orders ,根据题意模拟即可。

遍历结束后,我们将 buysell 中的订单数量相加,即为最终的积压订单数量。注意答案可能很大,需要对 109+710^9 + 7 取模。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nnorders 的长度。

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
class Solution:
    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:
        buy, sell = [], []
        for p, a, t in orders:
            if t == 0:
                while a and sell and sell[0][0] <= p:
                    x, y = heappop(sell)
                    if a >= y:
                        a -= y
                    else:
                        heappush(sell, (x, y - a))
                        a = 0
                if a:
                    heappush(buy, (-p, a))
            else:
                while a and buy and -buy[0][0] >= p:
                    x, y = heappop(buy)
                    if a >= y:
                        a -= y
                    else:
                        heappush(buy, (x, y - a))
                        a = 0
                if a:
                    heappush(sell, (p, a))
        mod = 10**9 + 7
        return sum(v[1] for v in buy + sell) % mod
speed

复杂度分析

指标
时间complexity is O(n log n) because each order may be pushed or popped from a heap at most once, and heap operations are log n. Space complexity is O(n) to store backlog orders in the heaps.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Mentions using heaps to simulate the order backlog efficiently.

  • question_mark

    Asks about matching orders greedily by price.

  • question_mark

    Questions about handling large order quantities without overflow.

warning

常见陷阱

外企场景
  • error

    Forgetting to process all batches in orders[i] when amounti > 1.

  • error

    Mixing up max heap for buy orders and min heap for sell orders.

  • error

    Incorrectly summing leftover orders leading to modulo mistakes.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Orders arriving in random order instead of sequentially by time.

  • arrow_right_alt

    Including additional order types beyond buy and sell.

  • arrow_right_alt

    Changing the matching rule to first-come-first-served instead of price priority.

help

常见问题

外企场景

积压订单中的订单总数题解:堆 | LeetCode #1801 中等