[TypeScript] Skew Heap
I like Skew Heap. It\'s much simpler than usual healps and easy to customise.\n\ntypescript\nconst MOD = 10 ** 9 + 7;\n\nclass Node {\n    constructor(\n       
I like Skew Heap. It\'s much simpler than usual healps and easy to customise.\n\n```typescript\nconst MOD = 10 ** 9 + 7;\n\nclass Node {\n    constructor(\n        public price: number,\n        public amount: number,\n        public left: Node | null = null,\n        public right: Node | null = null\n    ) { };\n}\n\n// Skew Heap\nfunction meld(a: Node | null, b: Node | null, maxheap: boolean): Node | null {\n    if (!a) return b;\n    if (!b) return a;\n    const cmp: boolean = maxheap ? a.price < b.price : a.price > b.price\n    if (cmp) [b, a] = [a, b];\t\t\n    a.right = meld(a.right, b, maxheap);\n    [a.left, a.right] = [a.right, a.left];\n    return a;\n}\n\nfunction getNumberOfBacklogOrders(orders: number[][]): number {\n    let buyBacklog: Node | null = null;\n    let sellBacklog: Node | null = null;\n\n    for (let [price, amount, orderType] of orders) {\n        if (orderType == 0) {\n            // buy order\n            while (amount > 0) {\n                if (!sellBacklog || sellBacklog.price > price) {\n                    buyBacklog = meld(buyBacklog, new Node(price, amount), true);\n                    break;\n                }\n                else {\n                    const am = Math.min(amount, sellBacklog.amount);\n                    amount -= am;\n                    const cur = new Node(sellBacklog.price, sellBacklog.amount - am);\n                    sellBacklog = meld(sellBacklog.left, sellBacklog.right, false)\n                    if (cur.amount > 0) {\n                        sellBacklog = meld(sellBacklog, cur, false);\n                    }\n                }\n            }\n        } else if (orderType == 1) {\n            // sell order\n            while (amount > 0) {\n                if (!buyBacklog || buyBacklog.price < price) {\n                    sellBacklog = meld(sellBacklog, new Node(price, amount), false);\n                    break;\n                }\n                else {\n                    const am = Math.min(amount, buyBacklog.amount);\n                    amount -= am;\n                    const cur = new Node(buyBacklog.price, buyBacklog.amount - am);\n                    buyBacklog = meld(buyBacklog.left, buyBacklog.right, true)\n                    if (cur.amount > 0) {\n                        buyBacklog = meld(buyBacklog, cur, true);\n                    }\n                }\n            }\n        }\n    }\n\n    // console.log({ buyBacklog, sellBacklog })\n\n    const countHeap = (n: Node | null): number => {\n        if (!n) return 0;\n        let amount = n.amount;\n        amount += countHeap(n.left) + countHeap(n.right);\n        amount %= MOD;\n        return amount;\n    }\n\n    return (countHeap(buyBacklog) + countHeap(sellBacklog)) % MOD;\n}\n\n```
[]