Number of Orders in the Backlog LeetCode solution in Python
1# Runtime: 689 ms (Top 70.7%) | Memory: 55.89 MB (Top 32.0%)234class Solution:5def getNumberOfBacklogOrders(self, orders):6b, s = [], []7heapq.heapify(b)8heapq.heapify(s)910for p, a, o in orders:11if o == 0:12heapq.heappush(b, [-p, a])1314elif o == 1:15heapq.heappush(s, [p, a])1617# Check "good" condition18while s and b and s[0][0] <= -b[0][0]:19a1, a2 = b[0][1], s[0][1]2021if a1 > a2:22b[0][1] -= a223heapq.heappop(s)24elif a1 < a2:25s[0][1] -= a126heapq.heappop(b)27else:28heapq.heappop(b)29heapq.heappop(s)3031count = sum([a for p, a in b]) + sum([a for p, a in s])32return count % (10**9 + 7)