Using Priority Queue
Intuition\n Describe your first thoughts on how to solve this problem. \nImplement two priority queues to manage orders: one for buy orders sorted in descending
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImplement two priority queues to manage orders: one for buy orders sorted in descending order and another for sell orders sorted in ascending order. When both buy and sell conditions are satisfied, begin removing the order from the respective priority queue.\n# Approach\n<!-- Describe your approach to solving the problem. -->\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```\nclass Solution {\n    private val MODULO = 1_000_000_007\n    fun getNumberOfBacklogOrders(orders: Array<IntArray>): Int {\n        val buy = PriorityQueue<IntArray>(compareBy {-it[0]})\n        val sell = PriorityQueue<IntArray>(compareBy {it[0]})\n\n        orders.forEach{\n            if(it[2] == 0){\n                if(sell.isEmpty()) buy.offer(it)\n                else{\n                    if (it[0] >= sell.peek()[0]){\n                        var quantities = it[1]\n                        while( quantities > 0 && sell.isNotEmpty() && it[0] >= sell.peek()[0]){\n                            val sellOrder = sell.poll()\n                            if( quantities < sellOrder[1]){\n                                sell.offer(intArrayOf(sellOrder[0], sellOrder[1] - quantities, sellOrder[2]))\n                                quantities = 0\n                            } else{\n                                quantities -= sellOrder[1]\n                            }\n                        }\n                        if( quantities > 0) buy.offer(intArrayOf(it[0], quantities, it[2]))\n                    } else  buy.offer(it)\n                }\n            } else{\n                if( buy.isEmpty()) sell.offer(it)\n                else{\n                    if(it[0] <= buy.peek()[0]){\n                        var quantities = it[1]\n                        while( quantities > 0 && buy.isNotEmpty() && it[0] <= buy.peek()[0]){\n                            val buyOrder = buy.poll()\n                            if( quantities < buyOrder[1]){\n                                buy.offer(intArrayOf(buyOrder[0], buyOrder[1] - quantities, buyOrder[2]))\n                                quantities = 0\n                            } else quantities -= buyOrder[1]\n                        }\n                        if(quantities > 0) sell.offer(intArrayOf(it[0], quantities, it[2]))\n\n\n                    } \n                    else {\n                        sell.offer(it)\n                    } \n\n                }\n\n\n            }\n\n\n\n        }\n\n        var res = 0 \n\n        while(buy.isNotEmpty()){\n            res += buy.poll()[1]\n            res %= MODULO\n        }\n\n        while(sell.isNotEmpty()){\n            res += sell.poll()[1]\n            res %= MODULO\n        }\n\n        return res\n    }\n\n}\n```
['Heap (Priority Queue)', 'Kotlin']