Multiset Solution | Clean Code | Easy to Understand
Idea?\n Track the pairs {price,amount} for sell orders and buy orders seperately in a multiset.\n Whenever we have a buy order, keep popping out the selling ord
**Idea?**\n* Track the pairs **{price,amount**} for sell orders and buy orders seperately in a **multiset**.\n* Whenever we have a *buy orde*r, **keep popping out the selling orders** present in the backlog that are less than or equal to current price and number of orders left is still positive.\n* Also, Whenever we have a *sell orde*r, **keep popping out the buying orders** present in the backlog that are greater than or equal to current price and number of orders is still positive.\n* Finally, add all the number of amounts that are present in the selling as well as buying backlog.\n\n```\nclass Solution {\npublic:\n\t// Time Complexity:- O(NlogN)\n\t// Space Complexity:- O(N)\n    #define ll long long\n    #define MOD 1000000007\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        ll ans = 0;\n        multiset<pair<int,int>> sell;\n        multiset<pair<int,int>,greater<pair<int,int>>> buy;\n        for(auto& order:orders){\n            int price = order[0],amount = order[1],orderType = order[2];\n            if(orderType==0){\n                while(!sell.empty() and amount>0 and sell.begin()->first<=price){\n                    int available_price = sell.begin()->first;\n                    int available_amount = sell.begin()->second;\n                    sell.erase(sell.begin());\n                    \n                    int take = min(amount,available_amount);\n                    \n                    amount -= take;\n                    available_amount -= take;\n                    \n                    if(available_amount){\n                        sell.insert({available_price,available_amount});\n                    }\n                }\n                \n                if(amount){\n                    buy.insert({price,amount});\n                }\n            }\n            else{\n                while(!buy.empty() and amount>0 and buy.begin()->first>=price){\n                    int available_price = buy.begin()->first;\n                    int available_amount = buy.begin()->second;\n                    buy.erase(buy.begin());\n                    \n                    int take = min(amount,available_amount);\n                    \n                    amount -= take;\n                    available_amount -= take;\n                    \n                    if(available_amount){\n                        buy.insert({available_price,available_amount});\n                    }\n                }\n                \n                if(amount){\n                    sell.insert({price,amount});\n                }\n            }\n        }\n        for(auto& x:buy){\n            ans += x.second;\n            ans %= MOD;\n        }\n        for(auto& x:sell){\n            ans += x.second;\n            ans %= MOD;\n        }\n        return (int)ans;\n    }\n};\n```\n**DOn\'t Forget to Upvote!**
['C']