[JavaScript] 1801. Number of Orders in the Backlog
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nImplementation logic\n\n\nthere are some orders\n\nan order can buy ( wit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nImplementation logic\n\n```\nthere are some orders\n\nan order can buy ( with some buy price say 1$, buy count as 10 )\nan order can sell ( with some sell price say 1$, sell count as 10 )\n\nif order says buy\n  if someone is selling <= buy price, then buy it\n      buy how many we want\n        if some are remaining, put it back to seller\n  while doing start from least seller\n      buy all you can\n        then go to another seller which is selling least\n  until we are done with buy all we want to buy\n  if we cannot buy all the count we need\n      then we add ourselves to buyers in future with price & remaining count\n\nif order says sell\n  similar to above\n    but reverse\n      buy<->sell, least will be max, etc\n\nonce all the order are processed\n  how many count of orders are remaining to buy or sell?\n    sum them all & return ( this left over is called backlog )\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction getNumberOfBacklogOrders(orders) {\n  const MOD = 1e9 + 7\n  const BUY = 0\n  const SELL = 1\n  const minSellers = new PriorityQueue({ compare: (a, b) => a[0] - b[0] })\n  const maxBuyers = new PriorityQueue({ compare: (a, b) => b[0] - a[0] })\n  for (const [currentOrderPrice, currentOrders, orderType] of orders) {\n    switch (orderType) {\n      case BUY:\n        {\n          let [buyMaxPriceForEachUnit, buyCount] = [currentOrderPrice, currentOrders]\n          while (buyCount > 0 && !minSellers.isEmpty()\n                 && minSellers.front()[0] <= buyMaxPriceForEachUnit) {\n            const [sellerPrice, sellerOrderCount] = minSellers.dequeue()\n            const buyCountPossible = Math.min(buyCount, sellerOrderCount)\n            buyCount -= buyCountPossible\n            const sellerOrderCountRemaining = sellerOrderCount - buyCountPossible\n            if (sellerOrderCountRemaining > 0) {\n              minSellers.enqueue([sellerPrice, sellerOrderCountRemaining])\n            }\n          }\n          if (buyCount > 0) maxBuyers.enqueue([buyMaxPriceForEachUnit, buyCount])\n        }\n        break\n      case SELL:\n        {\n          let [sellMinPriceForEachUnit, sellCount] = [currentOrderPrice, currentOrders]\n          while (sellCount > 0 && !maxBuyers.isEmpty()\n                 && maxBuyers.front()[0] >= sellMinPriceForEachUnit) {\n            const [buyerPrice, buyerOrderCount] = maxBuyers.dequeue()\n            const sellCountPossible = Math.min(sellCount, buyerOrderCount)\n            sellCount -= sellCountPossible\n            const buyerOrderCountRemaining = buyerOrderCount - sellCountPossible\n            if (buyerOrderCountRemaining > 0) {\n              maxBuyers.enqueue([buyerPrice, buyerOrderCountRemaining])\n            }\n          }\n          if (sellCount > 0) {\n            minSellers.enqueue([sellMinPriceForEachUnit, sellCount])\n          }\n        }\n        break\n    }\n  }\n  let backlog = 0\n  while (!maxBuyers.isEmpty()) {\n    const [buyerPrice, buyerOrderCount] = maxBuyers.dequeue()\n    backlog += buyerOrderCount\n    backlog %= MOD\n  }\n  while (!minSellers.isEmpty()) {\n    const [sellerPrice, sellerOrderCount] = minSellers.dequeue()\n    backlog += sellerOrderCount\n    backlog %= MOD\n  }\n  return backlog\n}\n\n```
['JavaScript']