Match Buy and Sell Batches and Count the Backlog. Process orders in input order, with rows [price,amount,type] and type 0 for buys or 1 for sells. A buy matches the cheapest pending sell priced no higher than the buy; a sell matches the highest pending buy priced no lower than the sell.
### Problem
Process `orders` in input order, with rows `[price,amount,type]` and type 0 for buys or 1 for sells. A buy matches the cheapest pending sell priced no higher than the buy; a sell matches the highest pending buy priced no lower than the sell. Each match removes one order from both sides. Unmatched quantities stay pending. Return the total pending amount modulo `10^9+7`.
```cpp
class Solution {
public:
int getNumberOfBacklogOrders(vector<vector<int>>& orders);
};
```
### Examples
**Example 1:**
```text
orders = [[5,3,0],[4,2,1],[6,4,1]]
Output: 5
Explanation: The price-4 sells remove two of the three buys. One price-5 buy and four price-6 sells remain.
```
**Example 2:**
```text
orders = [[8,4,1],[10,2,0],[7,1,0],[8,3,0]]
Output: 2
Explanation: The first buy consumes two sells, the price-7 buy waits, and the final buy consumes the other two sells and leaves one buy.
```
### Constraints
- `1 <= orders.length <= 10^5`.
- Each row has three entries.
- Price and amount are integers in `[1,10^9]`.
- Type is 0 or 1.
### Clarifying Questions
All units of one batch arrive before the next batch. Amount counts individual orders, not the number of batches or heap entries.
```hint Inspect the structure
Which pending price has priority for each incoming type, and what remains after a partial match?
```
### Reasoning checkpoints
- Match counterparties according to the required price priorities and arrival order.
- Preserve unmatched quantities after partial fills and compute the final count with safe arithmetic.
### Optional extensions
How could per-price ordered maps aggregate multiple equal-price batches?
Overview: Match Buy and Sell Batches and Count the Backlog. Process orders in input order, with rows [price,amount,type] and type 0 for buys or 1 for sells. A buy matches the cheapest pending sell priced no higher than the buy; a sell matches the highest pending buy priced no lower than the sell.
Process orders in input order, with rows [price,amount,type] and type 0 for buys or 1 for sells. A buy matches the cheapest pending sell priced no higher than the buy; a sell matches the highest pending buy priced no lower than the sell. Each match removes one order from both sides. Unmatched quantities stay pending. Return the total pending amount modulo 10^9+7.
class Solution {
public:
int getNumberOfBacklogOrders(vector<vector<int>>& orders);
};
Examples
Example 1:
orders = [[5,3,0],[4,2,1],[6,4,1]]
Output: 5
Explanation: The price-4 sells remove two of the three buys. One price-5 buy and four price-6 sells remain.
Example 2:
orders = [[8,4,1],[10,2,0],[7,1,0],[8,3,0]]
Output: 2
Explanation: The first buy consumes two sells, the price-7 buy waits, and the final buy consumes the other two sells and leaves one buy.
Constraints
1 <= orders.length <= 10^5
.
Each row has three entries.
Price and amount are integers in
[1,10^9]
.
Type is 0 or 1.
Clarifying Questions Guidance
All units of one batch arrive before the next batch. Amount counts individual orders, not the number of batches or heap entries.
Reasoning checkpoints
Match counterparties according to the required price priorities and arrival order.
Preserve unmatched quantities after partial fills and compute the final count with safe arithmetic.
Optional extensions
How could per-price ordered maps aggregate multiple equal-price batches?