0%

Leetcode:1801 积压订单中的订单总数

由于工作语言是Go,所以就用Go来解答,刚好熟悉一下Go相关的知识,这里主要是碰到了Go实现大小堆,具体的思路已完成,Go大小堆知识待补充。

题目链接:https://leetcode.com/problems/number-of-orders-in-the-backlog/

You are given a 2D integer array orders, where each orders[i] = [price<sub>i</sub>, amount<sub>i</sub>, orderType<sub>i</sub>] denotes that amount<sub>i</sub>~ ~orders have been placed of type orderType<sub>i</sub> at the price price<sub>i</sub>. The orderType<sub>i</sub> 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 amount<sub>i</sub> 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 10<sup>9</sup> + 7.

Constraints:

  • 1 <= orders.length <= 10<sup>5</sup>
  • orders[i].length == 3
  • 1 <= price<sub>i</sub>, amount<sub>i</sub> <= 10<sup>9</sup>
  • orderType<sub>i</sub> is either 0 or 1.

解题思路

这是一个优先队列模拟的问题,存储当前的积压订单,在新的订单到来的时候根据类型,若来的是购买订单,则找出价格最低的销售订单,且当销售订单的价格<=购买订单的价格时,进行匹配并相应消除;反之购买订单>=销售订单,进行消除。

要快速找到最小最大的订单价格,一个是插入时对slice进行sort排序,第二个就是使用大小堆进行判断,思路很简单,代码如下,主要是GO的大小堆实现卡住了我还蛮久的时间。。。相应博客指路 =>

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
func getNumberOfBacklogOrders(orders [][]int) int {
sHp, bHp := &sellHp{}, &buyHp{}
heap.Init(sHp)
heap.Init(bHp)
for _, o := range orders {
price, amount, orderType := o[0], o[1], o[2]
switch orderType {
case 0:
for amount > 0 && sHp.Len() > 0 && (*sHp)[0].price <= price {
if (*sHp)[0].amount >= amount {
(*sHp)[0].amount -= amount
amount = 0
break
}
amount -= heap.Pop(sHp).(order).amount
}
heap.Push(bHp, order{price, amount})
case 1:
for amount > 0 && bHp.Len() > 0 && (*bHp)[0].price >= price {
if (*bHp)[0].amount >= amount {
(*bHp)[0].amount -= amount
amount = 0
break
}
amount -= heap.Pop(bHp).(order).amount
}
heap.Push(sHp, order{price, amount})
}
}
res := 0
for _, order := range *sHp {
res += order.amount
}
for _, order := range *bHp {
res += order.amount
}
return res % (1e9 + 7)
}

type order struct{
price int
amount int
}

type sellHp []order

func (s sellHp) Len() int{ return len(s) }
func (s sellHp) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sellHp) Less(i, j int) bool { return s[i].price < s[j].price }

func (s *sellHp) Push(x interface{}) {
*s = append(*s, x.(order))
}

func (s *sellHp) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}

func (b buyHp) Len() int { return len(b) }
func (b buyHp) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b buyHp) Less(i, j int) bool { return b[i].price > b[j].price }

func (b *buyHp) Push(x interface{}) {
*b = append(*b, x.(order))
}

type buyHp []order

func (b *buyHp) Pop() interface{} {
old := *b
n := len(old)
x := old[n-1]
*b = old[0 : n-1]
return x
}