题目描述
给你一个二维整数数组 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 将会添加到积压订单中。 输入所有订单后,返回积压订单中的 订单总数 。由于数字可能很大,所以需要返回对 10 ** 9 + 7 取余的结果。
解题思路
算法
模拟
数据结构
大顶堆 + 小顶堆,大顶堆存放 buy,小顶堆存放 sell,这么做是为了获得最大的利润,即有人花大价钱从我这里买别人要卖的很便宜的东西
过程
每次来一个 buy 或 sell 的 order,我们都要减掉另外一个 type 堆顶订单以及本次遍历到的 order 的数量,这个数量当然是两者的最小值
然后根据两者剩余的情况,去看是否要 extract / insert
代码
/**
* @param {number[][]} orders
* @return {number}
*
*/
var getNumberOfBacklogOrders = function (orders) {
const N = 10 ** 9 + 7
const sellHeap = new Heap((a, b) => {
if (!b) return false
return a[0] > b[0]
})
const buyHeap = new Heap((a, b) => {
if (!b) return false
return a[0] < b[0]
})
// price amount type
for (const order of orders) {
// buy
if (order[2] === 0) {
while (
order[1] !== 0 &&
!sellHeap.isEmpty() &&
sellHeap.top()[0] <= order[0]
) {
const cnt = Math.min(sellHeap.top()[1], order[1])
order[1] -= cnt
sellHeap.top()[1] -= cnt
if (!sellHeap.top()[1]) sellHeap.extract()
}
if (order[1]) {
buyHeap.insert(order)
}
} else {
// sell
while (
order[1] !== 0 &&
!buyHeap.isEmpty() &&
buyHeap.top()[0] >= order[0]
) {
const cnt = Math.min(buyHeap.top()[1], order[1])
order[1] -= cnt
buyHeap.top()[1] -= cnt
if (!buyHeap.top()[1]) buyHeap.extract()
}
if (order[1]) {
sellHeap.insert(order)
}
}
}
let ans = 0
for (const x of sellHeap.heap) {
ans = (ans + x[1]) % N
}
for (const x of buyHeap.heap) {
ans = (ans + x[1]) % N
}
return ans
}
class Heap {
constructor(compareFn) {
this.heap = []
this.compareFn = compareFn
}
getLeftIndex(index) {
return index * 2 + 1
}
getRightIndex(index) {
return index * 2 + 2
}
getParentIndex(index) {
return Math.floor((index - 1) / 2)
}
size() {
return this.heap.length
}
isEmpty() {
return this.size() === 0
}
swap(parent, index) {
const arr = this.heap
;[arr[parent], arr[index]] = [arr[index], arr[parent]]
}
insert(value) {
const index = this.size()
this.heap.push(value)
this.siftUp(index)
}
siftUp(index) {
let parent = this.getParentIndex(index)
while (index > 0 && this.compareFn(this.heap[parent], this.heap[index])) {
this.swap(parent, index)
index = parent
parent = this.getParentIndex(index)
}
}
extract() {
if (this.isEmpty()) return
if (this.size() === 1) return this.heap.pop()
const removedItem = this.heap[0]
this.heap[0] = this.heap.pop()
this.siftDown(0)
return removedItem
}
siftDown(index) {
let element = index
const left = this.getLeftIndex(index)
const right = this.getRightIndex(index)
if (
index < this.size() &&
this.compareFn(this.heap[element], this.heap[left])
) {
element = left
}
if (
index < this.size() &&
this.compareFn(this.heap[element], this.heap[right])
) {
element = right
}
if (index !== element) {
this.swap(element, index)
this.siftDown(element)
}
}
top() {
return this.heap[0]
}
}