Using Sorted dictionary
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\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```\npublic class Solution {\n    public int GetNumberOfBacklogOrders(int[][] orders) {\n        var buyOrders = new SortedDictionary<long, long>(Comparer<long>.Create((x, y) => y.CompareTo(x)));\n\n        var sellOrders = new SortedDictionary<long, long>();\n\n        foreach(var bid in orders){\n            long price = bid[0];\n            long amount = bid[1];\n            var type = bid[2];\n\n            if(type == 0){ // buy order\n                while(amount > 0 && sellOrders.Count > 0 && sellOrders.First().Key <= price \n                )\n                {\n                    var sell = sellOrders.First();\n\n                    var sellAmount = Math.Min(amount, sell.Value);\n                    amount -= sellAmount;\n                    if(sell.Value == sellAmount){\n                        //buyOrders.Add(price, amount - sellAmount); \n                        sellOrders.Remove(sell.Key);\n                    }else{\n                        sellOrders[sell.Key] = sellOrders[sell.Key] - sellAmount;\n                    }\n                }\n\n                if(amount > 0){\n                    buyOrders[price] = buyOrders.GetValueOrDefault(price) + amount;\n                }\n            }else{\n                 while(amount > 0 && buyOrders.Count > 0 && buyOrders.First().Key >= price)\n                {\n                    var buy = buyOrders.First();\n\n                    var buyAmount = Math.Min(amount, buy.Value);\n                    amount -= buyAmount;\n                    if(buy.Value == buyAmount){\n                        //sellOrders.Add(price, amount - buyAmount); \n                        buyOrders.Remove(buy.Key);\n                    }else{\n                        buyOrders[buy.Key] = buyOrders[buy.Key] - buyAmount;\n                    }\n                }\n\n                if(amount > 0){\n                    sellOrders[price] = sellOrders.GetValueOrDefault(price) + amount;\n                }\n            }\n\n        }\n        return (int)((sellOrders.Values.Sum() + buyOrders.Values.Sum()) % (Math.Pow(10, 9) + 7));\n\n    }\n}\n```
['C#']