Number of Orders in the Backlog LeetCode solution in Python

1
# Runtime: 689 ms (Top 70.7%) | Memory: 55.89 MB (Top 32.0%)
2

3

4
class Solution:
5
def getNumberOfBacklogOrders(self, orders):
6
b, s = [], []
7
heapq.heapify(b)
8
heapq.heapify(s)
9

10
for p, a, o in orders:
11
if o == 0:
12
heapq.heappush(b, [-p, a])
13

14
elif o == 1:
15
heapq.heappush(s, [p, a])
16

17
# Check "good" condition
18
while s and b and s[0][0] <= -b[0][0]:
19
a1, a2 = b[0][1], s[0][1]
20

21
if a1 > a2:
22
b[0][1] -= a2
23
heapq.heappop(s)
24
elif a1 < a2:
25
s[0][1] -= a1
26
heapq.heappop(b)
27
else:
28
heapq.heappop(b)
29
heapq.heappop(s)
30

31
count = sum([a for p, a in b]) + sum([a for p, a in s])
32
return count % (10**9 + 7)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0