[JavaScript] 2 Heap Solution w/ explanation
javascript\nvar getNumberOfBacklogOrders = function(orders) {\n    const buyHeap = new Heap((child, parent) => child.price > parent.price)\n    const sellHeap =
```javascript\nvar getNumberOfBacklogOrders = function(orders) {\n    const buyHeap = new Heap((child, parent) => child.price > parent.price)\n    const sellHeap = new Heap((child, parent) => child.price < parent.price)\n    \n    for (let [price, amount, orderType] of orders) {\n        \n        // sell\n        if (orderType) {\n            \n            // while there are amount to be decremented from the sell,\n            // orders to in the buy backlog, and the price of the largest\n            // price is greater than the sell price decrement the\n            // amount of the order with the largest price by the amount\n            while (amount > 0 && buyHeap.peak() && buyHeap.peak().price >= price) {\n                if (buyHeap.peak().amount > amount) {\n                    buyHeap.peak().amount -= amount\n                    amount = 0\n                } else {\n                    amount -= buyHeap.pop().amount\n                }\n            }\n            \n            // if there is any amount left, add it to the sale backlog\n            if (amount) {\n                sellHeap.push({ price, amount })\n            }\n\n        // buy\n        } else {\n            \n            // while there are amount to be decremented from the buy,\n            // orders to in the sell backlog, and the price of the smallest\n            // price is less than the buy price decrement the\n            // amount of the order with the smallest price by the amount\n            while (amount > 0 && sellHeap.peak() && sellHeap.peak().price <= price) {\n                if (sellHeap.peak().amount > amount) {\n                    sellHeap.peak().amount -= amount\n                    amount = 0\n                } else {\n                    amount -= sellHeap.pop().amount;\n                }\n            }\n            \n            // if there is any amount left, add it to the buy backlog\n            if (amount) {\n                buyHeap.push({ price, amount })\n            }\n        }\n    }\n    \n    // total all of the amounts in both backlogs and return the result\n    let accumultiveAmount = 0;\n    for (const { amount } of buyHeap.store) {\n        accumultiveAmount += amount;\n    }\n    for (const { amount } of sellHeap.store) {\n        accumultiveAmount += amount;\n    }\n    return accumultiveAmount % 1000000007\n};\n\nclass Heap {\n    constructor(fn) {\n        this.store = [];\n        this.fn = fn;\n    }\n    \n    peak() {\n        return this.store[0];\n    }\n    \n    size() {\n        return this.store.length;\n    }\n    \n    pop() {\n        if (this.store.length < 2) {\n            return this.store.pop();\n        }\n        const result = this.store[0];\n        this.store[0] = this.store.pop();\n        this.heapifyDown(0);\n        return result;\n    }\n    \n    push(val) {\n        this.store.push(val);\n        this.heapifyUp(this.store.length - 1);\n    }\n    \n    heapifyUp(child) {\n        while (child) {\n            const parent = Math.floor((child - 1) / 2);\n            if (this.shouldSwap(child, parent)) {\n                [this.store[child], this.store[parent]] = [this.store[parent], this.store[child]]\n                child = parent;\n            } else {\n                return child;\n            }\n        }\n    }\n    \n    heapifyDown(parent) {\n        while (true) {\n            let [child, child2] = [1,2].map((x) => parent * 2 + x).filter((x) => x < this.size());\n            if (this.shouldSwap(child2, child)) {\n                child = child2\n            }\n            if (this.shouldSwap(child, parent)) {\n                [this.store[child], this.store[parent]] = [this.store[parent], this.store[child]]\n                parent = child;\n            } else {\n                return parent;\n            }\n        }\n    }\n    \n    shouldSwap(child, parent) {\n        return child && this.fn(this.store[child], this.store[parent]);\n    }\n}\n```
['Heap (Priority Queue)', 'JavaScript']