Wouldn't the time complexity be O(N^2 logN) using priority queues?
IntuitionUsing two priority queues like most of the solutions posted here.ApproachComplexity
Time complexity:
Inside the for loop, there is a while loop that mi
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Using two priority queues like most of the solutions posted here.

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Inside the for loop, there is a while loop that might iterate all existing elements in the priority queue (which would exist $O(n)$ of them). In that case, wouldn't the time complexity be $O(N^2 logN)$?  

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
To simulate the process of iterating over the orders:
```python3 []
class Solution:
    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:
        buyMaxHeap, sellMinHeap = [], []
        for p, k, t in orders:
            if t == 0: ## buy order
                while sellMinHeap and sellMinHeap[0][0] <= p and k > 0:
                    sell = heapq.heappop(sellMinHeap)
                    if sell[1] <= k:
                        k -= sell[1]
                    else:
                        heapq.heappush(sellMinHeap, (sell[0], sell[1]-k))
                        k = 0
                        break
                if k > 0:
                    heapq.heappush(buyMaxHeap, (-p, k))
            else:  ## sell order
                while buyMaxHeap and -buyMaxHeap[0][0] >= p and k > 0:
                    buy = heapq.heappop(buyMaxHeap)
                    if buy[1] <= k:
                        k -= buy[1]
                    else:
                        heapq.heappush(buyMaxHeap, (buy[0], buy[1]-k))
                        k = 0
                        break
                if k > 0:
                    heapq.heappush(sellMinHeap, (p, k))

        MOD = 10**9+7
        return sum([k for p, k in buyMaxHeap + sellMinHeap]) % MOD
```

For conciseness and readability:
```
class Solution:
    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:
        buyMaxHeap, sellMinHeap = [], []
        for p, k, t in orders:
            if t == 0: ## buy order
                heapq.heappush(buyMaxHeap, (-p, k))
            else:
                heapq.heappush(sellMinHeap, (p, k))
            while buyMaxHeap and sellMinHeap and -buyMaxHeap[0][0] >= sellMinHeap[0][0]:
                buy, sell = heapq.heappop(buyMaxHeap), heapq.heappop(sellMinHeap)
                if buy[1] > sell[1]:
                    heapq.heappush(buyMaxHeap, (buy[0], buy[1] - sell[1]))
                elif buy[1] < sell[1]:
                    heapq.heappush(sellMinHeap, (sell[0], sell[1] - buy[1]))

        MOD = 10**9+7
        return sum([k for p, k in buyMaxHeap + sellMinHeap]) % MOD

```
['Python3']