积压订单中的订单总数 Number of Orders in the Backlog
LeetCode传送门1801. 积压订单中的订单总数
原题
给你一个二维整数数组 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 取余的结果。
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 .
Example:
Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
Output: 6
Explanation: Here is what happens with the orders:
- 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
- 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
- 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
- 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4th order is added to the backlog.
Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.
Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
Output: 999999984
Explanation: Here is what happens with the orders:
- 109 orders of type sell with price 7 are placed. There are no buy orders, so the 109 orders are added to the backlog.
- 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.
- 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
- 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.
Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (10^9 + 7).
Constraints:
1 <= orders.length <= 10^5 orders[i].length == 3 1 <= pricei, amounti <= 10^9 is either 0 or 1.
思考线
解题思路
根据题意我们可以维护两个堆 大顶堆存buy订单,小顶堆存sell订单 每次来新订单时,与对应的堆顶订单比较并更新堆顶订单 最后统计结果时注意对中间结果取模
最重要的部分是构建堆,因为我们在之前做了很多堆的构建我在这里就不讲解了,有需要的同学可以看之前的每日一刷。
function getNumberOfBacklogOrders(orders: number[][]): number {
const buy = new Heap([], (p, c) => c[0] - p[0] > 0);
const sell = new Heap([], (p, c) => p[0] - c[0] > 0);
for (let i = 0; i < orders.length; i++) {
const item = orders[i];
if (item[2]) {
// sell
let peekBuy = buy.peek();
while (peekBuy && peekBuy[0] >= item[0] && item[1]) {
const b1 = peekBuy[1];
const s1 = item[1];
if (b1 <= s1) {
buy.poll();
item[1] -= b1;
if (b1 < s1) {
peekBuy = buy.peek();
}
} else {
peekBuy[1] -= s1;
item[1] -=s1;
}
}
if (item[1] > 0) {
sell.offer(item);
}
} else {
// buy
// buy.offer(item);
let peekSell = sell.peek();
while (peekSell && peekSell[0] <= item[0] && peekSell[1] && item[1]) {
const s1 = peekSell[1];
const b1 = item[1];
if (b1 >= s1) {
sell.poll();
item[1] -= s1;
if (b1 > s1) {
peekSell = sell.peek();
}
} else {
peekSell[1] -= b1;
item[1] -= b1;
}
}
if (item[1] > 0) {
buy.offer(item);
}
}
}
let res = 0;
buy.data.forEach(item => {
res += item[1];
})
sell.data.forEach(item => {
res += item[1];
})
return res% 1000000007;
};
class Heap {
data: number[];
comparator: (parent: number, child: number) => boolean;
constructor(data, comparator) {
this.data = data;
this.comparator = comparator;
this.heapify();
}
heapify() {
if (!this.size()) return;
for (let i = 1; i < this.size(); i++) {
this.bubbleUp(i);
}
}
bubbleUp(index) {
while (index > 0) {
const parent = (index - 1) >> 1;
if (this.comparator(this.data[parent], this.data[index])) {
this.swap(parent, index);
index = parent;
} else {
break;
}
}
}
bubbleDown(index) {
const len = this.size()
while (index < len) {
const leftIndex = index * 2 + 1;
const rightIndex = index * 2 + 2;
let findIndex = index;
if (leftIndex < len && this.comparator(this.data[findIndex], this.data[leftIndex])) {
findIndex = leftIndex
}
if (rightIndex < len && this.comparator(this.data[findIndex], this.data[rightIndex])) {
findIndex = rightIndex
}
if (index !== findIndex) {
this.swap(findIndex, index);
index = findIndex;
} else {
break;
}
}
}
swap(index1: number, index2: number) {
[this.data[index1], this.data[index2]] = [this.data[index2], this.data[index1]]
}
poll() {
if (!this.size()) return null;
const res = this.data.shift();
if (this.size()) {
this.data.unshift(this.data.pop())
this.bubbleDown(0)
}
return res;
}
offer(val) {
this.data.push(val);
this.bubbleUp(this.size() - 1);
}
peek() {
return this.data[0];
}
size() {
return this.data.length;
}
}
这道题的时间复杂度 O(NlogN)
这就是我对本题的解法,如果有疑问或者更好的解答方式,欢迎留言互动。