forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
26 lines (26 loc) · 917 Bytes
/
Solution.py
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
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