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 totalamount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo10^9 + 7.

Input: orders =[[10,5,0],[15,2,1],[25,1,1],[30,4,0]]Output: 6Explanation: 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 25is placed. There are no buy orders with prices larger than or equal to 25in 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 is15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is25 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 is6.

Input: orders =[[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]Output: 999999984Explanation: 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 is7, 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 is7, so the 999999995 orders are added to the backlog.-1 order of type sell with price 5is placed. It is matched with the buy order of the highest price, which is5, 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%(10^9+7).
We need to efficiently match buy and sell orders according to their prices. Buy orders match with the lowest-priced sell orders, and sell orders match with the highest-priced buy orders. We use two heaps: a max-heap for buy orders and a min-heap for sell orders, simulating the backlog and matching process.
import java.util.PriorityQueue
classSolution {
fungetNumberOfBacklogOrders(orders: Array<IntArray>): Int {
val MOD = 1_000_000_007
val buy = PriorityQueue<IntArray> { a, b -> b[0] - a[0] }
val sell = PriorityQueue<IntArray> { a, b -> a[0] - b[0] }
for (o in orders) {
var price = o[0]; var amount = o[1]; val type = o[2]
if (type ==0) {
while (amount > 0&& sell.isNotEmpty() && sell.peek()[0] <= price) {
val s = sell.poll()
val match = minOf(amount, s[1])
amount -= match; s[1] -= match
if (s[1] > 0) sell.offer(s)
}
if (amount > 0) buy.offer(intArrayOf(price, amount))
} else {
while (amount > 0&& buy.isNotEmpty() && buy.peek()[0] >= price) {
val b = buy.poll()
val match = minOf(amount, b[1])
amount -= match; b[1] -= match
if (b[1] > 0) buy.offer(b)
}
if (amount > 0) sell.offer(intArrayOf(price, amount))
}
}
var ans = 0Lwhile (buy.isNotEmpty()) ans = (ans + buy.poll()[1]) % MOD
while (sell.isNotEmpty()) ans = (ans + sell.poll()[1]) % MOD
return ans.toInt()
}
}
import heapq
classSolution:
defgetNumberOfBacklogOrders(self, orders: list[list[int]]) -> int:
MOD =10**9+7 buy = [] # max-heap sell = [] # min-heapfor price, amount, typ in orders:
if typ ==0:
while amount and sell and sell[0][0] <= price:
sp, sa = heapq.heappop(sell)
match= min(amount, sa)
amount -=match; sa -=matchif sa: heapq.heappush(sell, (sp, sa))
if amount: heapq.heappush(buy, (-price, amount))
else:
while amount and buy and-buy[0][0] >= price:
bp, ba = heapq.heappop(buy)
match= min(amount, ba)
amount -=match; ba -=matchif ba: heapq.heappush(buy, (bp, ba))
if amount: heapq.heappush(sell, (price, amount))
ans =0for _, a in buy: ans = (ans + a) % MOD
for _, a in sell: ans = (ans + a) % MOD
return ans
use std::collections::BinaryHeap;
impl Solution {
pubfnget_number_of_backlog_orders(orders: Vec<Vec<i32>>) -> i32 {
letmut buy = BinaryHeap::new(); // max-heap
letmut sell = std::collections::BinaryHeap::new_by(|a: &(i32, i32), b: &(i32, i32)| a.0.cmp(&b.0).reverse()); // min-heap
let m =1_000_000_007;
for o in orders {
let (price, mut amount, typ) = (o[0], o[1], o[2]);
if typ ==0 {
while amount >0&&!sell.is_empty() && sell.peek().unwrap().0<= price {
let (sp, sa) = sell.pop().unwrap();
let match_amt = amount.min(sa);
amount -= match_amt;
let sa = sa - match_amt;
if sa >0 { sell.push((sp, sa)); }
}
if amount >0 { buy.push((price, amount)); }
} else {
while amount >0&&!buy.is_empty() && buy.peek().unwrap().0>= price {
let (bp, ba) = buy.pop().unwrap();
let match_amt = amount.min(ba);
amount -= match_amt;
let ba = ba - match_amt;
if ba >0 { buy.push((bp, ba)); }
}
if amount >0 { sell.push((price, amount)); }
}
}
letmut ans =0i64;
whilelet Some((_, a)) = buy.pop() { ans = (ans + a asi64) % m; }
whilelet Some((_, a)) = sell.pop() { ans = (ans + a asi64) % m; }
ans asi32 }
}