TypeScript Clean Min & Max Heaps Solution
IntuitionKeep two heaps, max heap for buy, min heap for sell backlogs.ApproachJust simulate the process as explained in the question.Complexity

Time complexity
# Intuition
Keep two heaps, max heap for buy, min heap for sell backlogs.

# Approach
Just simulate the process as explained in the question.

# Complexity
- Time complexity:
O(N log N)

- Space complexity:
O(N)

# Code
```typescript []
function getNumberOfBacklogOrders(orders: number[][]): number {
    const buyMaxHeapBacklog = new MaxPriorityQueue({ priority: (tuple) => tuple[0] });
    const sellMinHeapBacklog = new MinPriorityQueue({ priority: (tuple) => tuple[0] });

    for (let [price, amount, type] of orders) {
        // Buy look for '<='
        if (type === 0) {
            while (!sellMinHeapBacklog.isEmpty() && amount > 0 && sellMinHeapBacklog.front().priority <= price) {
                const { priority, element } = sellMinHeapBacklog.dequeue();
                const [sellOrderPrice, sellOrderAmount] = element;
                const amountToSubstract = Math.min(sellOrderAmount, amount)
                const updatedSellOrderAmount = sellOrderAmount - amountToSubstract;
                amount -= amountToSubstract;
                if (updatedSellOrderAmount > 0) {
                    sellMinHeapBacklog.enqueue([sellOrderPrice, updatedSellOrderAmount]);
                }
            }

            if (amount > 0) {
                buyMaxHeapBacklog.enqueue([price, amount]);
            }
        }
        // Sell look for '>='
        else {
            while (!buyMaxHeapBacklog.isEmpty() && amount > 0 && buyMaxHeapBacklog.front().priority >= price) {
                const { priority, element } = buyMaxHeapBacklog.dequeue();
                const [buyOrderPrice, buyOrderAmount] = element;
                const amountToSubstract = Math.min(buyOrderAmount, amount)
                const updatedBuyOrderAmount = buyOrderAmount - amountToSubstract;
                amount -= amountToSubstract;
                if (updatedBuyOrderAmount > 0) {
                    buyMaxHeapBacklog.enqueue([buyOrderPrice, updatedBuyOrderAmount]);
                }
            }

            if (amount > 0) {
                sellMinHeapBacklog.enqueue([price, amount]);
            }
        }
    }

    let amountSum = 0;

    amountSum += sellMinHeapBacklog.toArray().reduce((sum, cur) => sum + cur.element[1], 0);
    amountSum += buyMaxHeapBacklog.toArray().reduce((sum, cur) => sum + cur.element[1], 0);

    const MOD = 1000000007;
    return amountSum % MOD;
};
```
['Array', 'Heap (Priority Queue)', 'Simulation', 'TypeScript']