You are given a 2D integer array orders, where each orders[i] = [pricei, amounti, orderTypei] denotes that amounti orders have been placed of type orderTypei at the price pricei. The orderTypei is:


	0 if it is a batch of buy orders, or
	1 if it is a batch of sell orders.


Note that orders[i] represents a batch of amounti independent orders with the same price and order type. All orders represented by orders[i] will be placed before all orders represented by orders[i+1] for all valid i.

There is a backlog that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:


	If the order is a buy order, you look at the sell order with the smallest price in the backlog. If that sell order's price is smaller than or equal to the current buy order's price, they will match and be executed, and that sell order will be removed from the backlog. Else, the buy order is added to the backlog.
	Vice versa, if the order is a sell order, you look at the buy order with the largest price in the backlog. If that buy order's price is larger than or equal to the current sell order's price, they will match and be executed, and that buy order will be removed from the backlog. Else, the sell order is added to the backlog.


Return the total amount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo 109 + 7.

&nbsp;
Example 1:

Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
Output: 6
Explanation: Here is what happens with the orders:
- 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
- 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
- 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
- 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4th order is added to the backlog.
Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.


Example 2:

Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
Output: 999999984
Explanation: Here is what happens with the orders:
- 109 orders of type sell with price 7 are placed. There are no buy orders, so the 109 orders are added to the backlog.
- 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.
- 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
- 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.
Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (109 + 7).


&nbsp;
Constraints:


	1 &lt;= orders.length &lt;= 105
	orders[i].length == 3
	1 &lt;= pricei, amounti &lt;= 109
	orderTypei is either 0 or 1.

class Solution:
    def getNumberOfBacklogOrders(self, orders):
        b, s = [], []
        heapq.heapify(b)
        heapq.heapify(s)
        
        for p,a,o in orders:
            if o == 0:
                heapq.heappush(b, [-p, a])
                
            elif o == 1:
                heapq.heappush(s, [p, a])
            
            # Check "good" condition
            while s and b and s[0][0] <= -b[0][0]:
                a1, a2 = b[0][1], s[0][1]
                
                if a1 > a2:
                    b[0][1] -= a2
                    heapq.heappop(s)
                elif a1 < a2:
                    s[0][1] -= a1
                    heapq.heappop(b)
                else:
                    heapq.heappop(b)
                    heapq.heappop(s)
                    
        count = sum([a for p,a in b]) + sum([a for p,a in s])
        return count % (10**9 + 7)
class Solution {
    
    PriorityQueue<Order> buyBackLog;
    PriorityQueue<Order> sellBackLog;
    
    static int MOD = 1_000_000_007;
    
    
    public int getNumberOfBacklogOrders(int[][] orders) {
        
        //max heap, heapify on price
        buyBackLog = new PriorityQueue<Order>((a, b) -> (b.price - a.price));
        //min heap, heapify on price
        sellBackLog = new PriorityQueue<Order>((a, b) -> (a.price - b.price));
        
        
        //handle all order
        for(int[] order : orders){
            int price     = order[0];
            int quantity  = order[1];
            int orderType = order[2];
            
            if(orderType == 0){
                //buy order 
                handleBuyOrder(new Order(price, quantity));
                
            }else if(orderType == 1){ 
                //sell order
                handleSellOrder(new Order(price, quantity));
            }
        }
        
        long counts = 0L;
        
        //count buy backlog
        while(!buyBackLog.isEmpty()){
            counts += buyBackLog.remove().quantity; 
            counts %= MOD;
        }
        
        //count sell backlog
        while(!sellBackLog.isEmpty()){
            counts += sellBackLog.remove().quantity; 
            counts %= MOD;
        }
        
        
        return (int) (counts % MOD);
    }
    
    
    
    
    private void handleBuyOrder(Order buyOrder){
        //just add buyorder, if there is no sell back log
        if(sellBackLog.isEmpty()){
            buyBackLog.add(buyOrder);
            return;
        }
        
        
        while(!sellBackLog.isEmpty() && buyOrder.price >= sellBackLog.peek().price && buyOrder.quantity > 0){
            //selloder with minumum price
            Order sellOrder = sellBackLog.remove();
            
            if(buyOrder.quantity >= sellOrder.quantity){
                buyOrder.quantity -= sellOrder.quantity;
                sellOrder.quantity = 0;
            } else {
                //decrement sell order, add remaining sellorder
                sellOrder.quantity -= buyOrder.quantity;
                sellBackLog.add(sellOrder);
                
                buyOrder.quantity = 0;
            }
        }
        
        //add reaming buyorder
        if(buyOrder.quantity > 0){
            buyBackLog.add(buyOrder);
        }
    }
    
    
    private void handleSellOrder(Order sellOrder){
        //just add sell order, if there is no buy backlog
        if(buyBackLog.isEmpty()){
            sellBackLog.add(sellOrder);
            return;
        }
        
        
        while(!buyBackLog.isEmpty() && buyBackLog.peek().price >= sellOrder.price && sellOrder.quantity > 0){
            //buy order with maximum price
            Order buyOrder = buyBackLog.remove();
            
            if(sellOrder.quantity >= buyOrder.quantity){
                sellOrder.quantity -= buyOrder.quantity;
                buyOrder.quantity = 0;
                
            }else{
                //decrement buy order quantity, add remaining buyorder
                buyOrder.quantity -= sellOrder.quantity;
                buyBackLog.add(buyOrder);
                
                sellOrder.quantity = 0;
            }
        }
        
        //add remaining sell order
        if(sellOrder.quantity > 0){
            sellBackLog.add(sellOrder);
        }
    }
}

class Order{
    int price;
    int quantity;
    
    public Order(int price, int quantity){
        this.price = price;
        this.quantity = quantity;
    }
}
class Solution {
public:
    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
        int n = orders.size();
        //0 - buy  , 1 - sell; 
        priority_queue<vector<int>> buyBacklog;
        priority_queue<vector<int> , vector<vector<int>> , greater<vector<int>>> sellBacklog;
        
        for(auto order : orders) {
            if(order[2] == 0) 
                buyBacklog.push(order);
            else 
                sellBacklog.push(order);
            
            while(!buyBacklog.empty() && !sellBacklog.empty() && sellBacklog.top()[0] <= buyBacklog.top()[0])  {
                auto btop = buyBacklog.top();
                buyBacklog.pop();
                auto stop = sellBacklog.top();
                sellBacklog.pop();
                int diff = btop[1] - stop[1];
                if(diff > 0) {
                    btop[1] = diff;
                    buyBacklog.push(btop);
                }
                else if(diff<0) {
                    stop[1] = abs(diff);
                    sellBacklog.push(stop);
                }
            }
        }
        
        int ans = 0 , mod = 1e9+7;
        while(!buyBacklog.empty()){
            ans = (ans +buyBacklog.top()[1])%mod;
            buyBacklog.pop();
        }
        while(!sellBacklog.empty()){
            ans = (ans+ sellBacklog.top()[1])%mod;
            sellBacklog.pop();
        }
        return ans;
    }
};
var getNumberOfBacklogOrders = function(orders) {
    const buyHeap = new Heap((child, parent) => child.price > parent.price)
    const sellHeap = new Heap((child, parent) => child.price < parent.price)
    
    for (let [price, amount, orderType] of orders) {
        
        // sell
        if (orderType) {
            
            // while there are amount to be decremented from the sell,
            // orders to in the buy backlog, and the price of the largest
            // price is greater than the sell price decrement the
            // amount of the order with the largest price by the amount
            while (amount > 0 && buyHeap.peak() && buyHeap.peak().price >= price) {
                if (buyHeap.peak().amount > amount) {
                    buyHeap.peak().amount -= amount
                    amount = 0
                } else {
                    amount -= buyHeap.pop().amount
                }
            }
            
            // if there is any amount left, add it to the sale backlog
            if (amount) {
                sellHeap.push({ price, amount })
            }

        // buy
        } else {
            
            // while there are amount to be decremented from the buy,
            // orders to in the sell backlog, and the price of the smallest
            // price is less than the buy price decrement the
            // amount of the order with the smallest price by the amount
            while (amount > 0 && sellHeap.peak() && sellHeap.peak().price <= price) {
                if (sellHeap.peak().amount > amount) {
                    sellHeap.peak().amount -= amount
                    amount = 0
                } else {
                    amount -= sellHeap.pop().amount;
                }
            }
            
            // if there is any amount left, add it to the buy backlog
            if (amount) {
                buyHeap.push({ price, amount })
            }
        }
    }
    
    // total all of the amounts in both backlogs and return the result
    let accumultiveAmount = 0;
    for (const { amount } of buyHeap.store) {
        accumultiveAmount += amount;
    }
    for (const { amount } of sellHeap.store) {
        accumultiveAmount += amount;
    }
    return accumultiveAmount % 1000000007
};

class Heap {
    constructor(fn) {
        this.store = [];
        this.fn = fn;
    }
    
    peak() {
        return this.store[0];
    }
    
    size() {
        return this.store.length;
    }
    
    pop() {
        if (this.store.length < 2) {
            return this.store.pop();
        }
        const result = this.store[0];
        this.store[0] = this.store.pop();
        this.heapifyDown(0);
        return result;
    }
    
    push(val) {
        this.store.push(val);
        this.heapifyUp(this.store.length - 1);
    }
    
    heapifyUp(child) {
        while (child) {
            const parent = Math.floor((child - 1) / 2);
            if (this.shouldSwap(child, parent)) {
                [this.store[child], this.store[parent]] = [this.store[parent], this.store[child]]
                child = parent;
            } else {
                return child;
            }
        }
    }
    
    heapifyDown(parent) {
        while (true) {
            let [child, child2] = [1,2].map((x) => parent * 2 + x).filter((x) => x < this.size());
            if (this.shouldSwap(child2, child)) {
                child = child2
            }
            if (this.shouldSwap(child, parent)) {
                [this.store[child], this.store[parent]] = [this.store[parent], this.store[child]]
                parent = child;
            } else {
                return parent;
            }
        }
    }
    
    shouldSwap(child, parent) {
        return child && this.fn(this.store[child], this.store[parent]);
    }
}
Number of Orders in the Backlog