C# 100% best runtime 90% best memory using only two priority queues
ProofIntuition + ApproachWe need to keep track of low sell price because problem says we need to match buys with the smallest sell price.We need to keep track o
# Proof

![image.png](https://assets.leetcode.com/users/images/6c0961e5-8f28-49d6-946e-89cd277c7f3d_1738704224.5854566.png)

# Intuition + Approach

We need to keep track of low sell price because problem says we need to match buys with the smallest sell price.

We need to keep track of high buy price to match it with sell prices.

Data strucutre that keep ordering in c# with payloads is PriorityQueue<T, V>.

There are 3 states for each new order:
1. We can't match it ? => we push it to its queue, if it buy we push it to buyQueue, if it is sell we push it sell queue,
2. We totaly match it:
    - Using one order.
    - Using multiple orders.
3. We partialy match it.

# Complexity
- Time complexity:
Inserting in priority queue is O(logn) and we iterate over all orders which is O(n) the total is O(nlong)

- Space complexity:
We can store up to n order which is O(n).

# Code
```csharp []
public class Solution {
    public int GetNumberOfBacklogOrders(int[][] orders)
    {
        var priotirySell = new PriorityQueue<int[], int>();
        var priorityBuy = new PriorityQueue<int[], int>(Comparer<int>.Create((x, y) => y.CompareTo(x)));

        long allAmount = 0;

        foreach(int[] order in orders)
        {
            bool isBuy = order[2] == 0;
            int price = order[0];
            int amount = order[1];
            var takeFrom = isBuy ? priotirySell : priorityBuy;
            var pushTo = isBuy ? priorityBuy : priotirySell;
            bool orderSatified = false;
            do{
                if(takeFrom.Count == 0)
                {
                    // we can't satisfy the order
                    orderSatified = true;
                    allAmount += amount;
                    pushTo.Enqueue(order, order[0]);
                }
                else
                {
                    var temp = takeFrom.Peek(); 
                    if((isBuy && temp[0] <= price) || (!isBuy && price <= temp[0]))
                    {
                        if(amount <= temp[1])
                        {
                            orderSatified = true;
                            allAmount -= amount;
                            if(amount - temp[1] == 0)
                            {
                                takeFrom.Dequeue();
                            }else{
                                temp[1] =  temp[1] - amount;
                            }

                        }else{
                            amount -= temp[1];
                            allAmount -= temp[1];
                            order[1] -= temp[1];
                            takeFrom.Dequeue();
                        }

                    }else
                    {
                        pushTo.Enqueue(order, order[0]);
                        allAmount += amount;
                        orderSatified = true;
                    }
                }

            }while(!orderSatified);
        }

        return (int)(allAmount % 1_000_000_007);
    }
}
```
['C#']