количество заказов в бэклоге
ИнтуицияПодходСложность
Временная сложность:

Сложность пространства:
Код
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
let getNumberOfBacklogOrders = function (orders) {

    let buy = []
    let sell = []
    let stop=true


    function sortBuyandSell(arr, elem) {

        let  start = 0;
        let end = arr.length;

        while (start < end) {
            const mid = Math.floor((start + end) / 2);
            if (arr[mid][0] < elem[0]) {
                start = mid + 1;
            } else {
                end = mid;
            }
        }

        arr.splice(start, 0, elem);
        return start;
    }

    function differenceAmount(num,indexBuy,indexSell){
       
       if(num>0){
       buy[indexBuy][1] =num
       sell.splice(indexSell,1)
        }
       else if(num<0){
        sell[indexSell][1]=Math.abs(num)
        buy.splice(indexBuy,1)
       }
       else{
         
         buy.splice(indexBuy,1) 
        sell.splice(indexSell,1) 
        stop = false
       }
    }
    
    function addBackLog(elem){
      
      
      
      if(elem[2]===0){
          if(sell.length > 0 && elem[0] >= sell[0][0]){
              let indexBuy=sortBuyandSell(buy,elem)
              while(stop && buy.length>indexBuy &&  sell.length > 0 && (buy[indexBuy][0] >=  sell[0][0])){
                  let num=buy[indexBuy][1]-sell[0][1]
                   differenceAmount(num,indexBuy,0)
                }
             stop=true
                
            }
            else {
                sortBuyandSell(buy,elem)
            }
        }
        else if(elem[2]===1){
            if(buy.length > 0 && elem[0] <=  buy[buy.length-1][0]){
                let indexSell=sortBuyandSell(sell,elem)
            while(stop && buy.length>0 &&  sell.length > indexSell && sell[indexSell][0] <=  buy[buy.length-1][0]){
                let num=buy[buy.length-1][1]-sell[indexSell][1]
                differenceAmount(num,buy.length-1,indexSell )
            }
          stop=true
        }
        else {
            sortBuyandSell(sell,elem)
        }
    }
      
    }
    

    for(let i=0;i<orders.length;i++){
       addBackLog(orders[i])
    }

 let buyAmount=buy.map(el=>el[1]).reduce((a,b)=>a+b,0)
 let sellAmount=sell.map(el=>el[1]).reduce((a,b)=>a+b,0)


 return (buyAmount+sellAmount) % (1000000000 + 7 )


};
```
['JavaScript']