> For the complete documentation index, see [llms.txt](https://zhenchaogan.gitbook.io/leetcode-solution/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-1801-number-of-orders-in-the-backlog.md).

# LeetCode 1801. Number of Orders in the Backlog

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`.

**Example 1:**![](https://assets.leetcode.com/uploads/2021/03/11/ex1.png)

```
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:**![](https://assets.leetcode.com/uploads/2021/03/11/ex2.png)

```
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).
```

**Constraints:**

* `1 <= orders.length <= 10^5`
* `orders[i].length == 3`
* `1 <= pricei, amounti <= 10^9`
* `orderTypei` is either `0` or `1`.

## Solution

```
class Solution {
public:
    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
        auto cmp1 = [](const vector<int> &a, const vector<int> &b) {
            return a[0] < b[0];
        };
        auto cmp2 = [](const vector<int> &a, const vector<int> &b) {
            return a[0] > b[0];
        };

        priority_queue<vector<int>, vector<vector<int>>, decltype(cmp1)> buy_backlog(cmp1);
        priority_queue<vector<int>, vector<vector<int>>, decltype(cmp2)> sell_backlog(cmp2);
        
        for (vector<int> order : orders) {
            if (order[2] == 0) {
                // buy order
                while (order[1] > 0 && !sell_backlog.empty()) {
                    if (sell_backlog.top()[0] > order[0]) {
                        break;
                    }
                    vector<int> sell_order = sell_backlog.top();
                    sell_backlog.pop();
                    
                    if (order[1] >= sell_order[1]) {
                        order[1] -= sell_order[1];
                    } else {
                        sell_order[1] -= order[1];
                        order[1] = 0;
                        sell_backlog.push(sell_order);
                    }
                }
                if (order[1] > 0) {
                    buy_backlog.push(order);
                }
            } else {
                while (order[1] > 0 && !buy_backlog.empty()) {
                    if (buy_backlog.top()[0] < order[0]) {
                        break;
                    }
                    vector<int> buy_order = buy_backlog.top();
                    buy_backlog.pop();
                    
                    if (order[1] >= buy_order[1]) {
                        order[1] -= buy_order[1];
                    } else {
                        buy_order[1] -= order[1];
                        order[1] = 0;
                        buy_backlog.push(buy_order);
                    }
                }
                if (order[1] > 0) {
                    sell_backlog.push(order);
                }
            }
        }
        
        int mod = 1e9+7;
        int ans = 0;
        while (!buy_backlog.empty()) {
            ans += buy_backlog.top()[1];
            ans %= mod;
            buy_backlog.pop();
        }
        while (!sell_backlog.empty()) {
            ans += sell_backlog.top()[1];
            ans %= mod;
            sell_backlog.pop();
        }
        return ans;
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-1801-number-of-orders-in-the-backlog.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
