Easy to understand JavaScript solution (Priority Queue)
Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n\nvar getNumberOfBacklogOrders = function(orders) {\n    const MODULO = 10 ** 9
# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nvar getNumberOfBacklogOrders = function(orders) {\n    const MODULO = 10 ** 9 + 7;\n    const buyQueue = new MaxPriorityQueue({ priority:x => x.price });\n    const sellQueue = new MinPriorityQueue({ priority:x => x.price });\n    const comparePrice = ({ isBuy, price, checkQueue }) => {\n        const backlogPrice = checkQueue.front().element.price;\n\n        return isBuy ? backlogPrice <= price : backlogPrice >= price;\n    };\n    let result = 0;\n    \n    for (let [price, amount, orderType] of orders) {\n        const isBuy = orderType === 0;\n        const currentQueue =  isBuy ? buyQueue : sellQueue;\n        const checkQueue = isBuy ? sellQueue : buyQueue;\n        \n        while (!checkQueue.isEmpty() && amount > 0 && comparePrice({ isBuy, price, checkQueue })) {\n            const order = checkQueue.dequeue().element;\n\n            if (amount >= order.amount) {\n                amount -= order.amount;\n            } else {\n                order.amount -= amount;\n                amount = 0;\n                checkQueue.enqueue(order);\n            }\n        }\n        if (amount === 0) continue;\n        currentQueue.enqueue({ price, amount });\n    }\n\n    while (!buyQueue.isEmpty()) result += buyQueue.dequeue().element.amount;\n    while (!sellQueue.isEmpty()) result += sellQueue.dequeue().element.amount;\n    return result % MODULO;\n};\n```
['JavaScript']