Tap here to switch tabs
Problem
Submissions

Problem: Number of Orders in the Backlog

med
30 min
Try to solve the Number of Orders in the Backlog problem.

Statement

You are given a 2extD2 ext{D} integer array orders, where each orders[i] = [price_i, amount_i, orderType_i] represents an order placed:

  • If orderType_i is 00, it is a buy order.

  • If orderType_i is 11, it is a sell order.

A backlog stores orders that have not yet been executed. When a new order arrives, it is processed as follows:

  • If it is a buy order, check the backlog for a sell order with the lowest price that is less than or equal to the buy order’s price. If such an order exists, the two orders are matched, reducing the quantities of both orders by the minimum of their remaining amounts. This process repeats until the buy order is completely filled or no valid matching sell order remains in the backlog. Any remaining unmatched amount from the buy order is added to the backlog.

  • If it is a sell order, check the backlog for a buy order with the highest price that is greater than or equal to the sell order’s price. If such an order exists, the two orders are matched, reducing the quantities of both orders by the minimum of their remaining amounts. This process repeats until the sell order is completely filled or no valid matching buy order remains in the backlog. Any remaining unmatched amount from the sell order is added to the backlog.

Return the total amount of orders remaining in the backlog after all orders have been processed. Since the answer may be large, return it modulo 109+710^9 + 7.

Constraints:

  • 1≤1 \leq orders.length ≤105\leq 10^5

  • orders[i].length =3= 3

  • 1≤1 \leq price_i, amount_i ≤109\leq 10^9

  • orderType_i is either 00 or 11

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number of Orders in the Backlog

med
30 min
Try to solve the Number of Orders in the Backlog problem.

Statement

You are given a 2extD2 ext{D} integer array orders, where each orders[i] = [price_i, amount_i, orderType_i] represents an order placed:

  • If orderType_i is 00, it is a buy order.

  • If orderType_i is 11, it is a sell order.

A backlog stores orders that have not yet been executed. When a new order arrives, it is processed as follows:

  • If it is a buy order, check the backlog for a sell order with the lowest price that is less than or equal to the buy order’s price. If such an order exists, the two orders are matched, reducing the quantities of both orders by the minimum of their remaining amounts. This process repeats until the buy order is completely filled or no valid matching sell order remains in the backlog. Any remaining unmatched amount from the buy order is added to the backlog.

  • If it is a sell order, check the backlog for a buy order with the highest price that is greater than or equal to the sell order’s price. If such an order exists, the two orders are matched, reducing the quantities of both orders by the minimum of their remaining amounts. This process repeats until the sell order is completely filled or no valid matching buy order remains in the backlog. Any remaining unmatched amount from the sell order is added to the backlog.

Return the total amount of orders remaining in the backlog after all orders have been processed. Since the answer may be large, return it modulo 109+710^9 + 7.

Constraints:

  • 1≤1 \leq orders.length ≤105\leq 10^5

  • orders[i].length =3= 3

  • 1≤1 \leq price_i, amount_i ≤109\leq 10^9

  • orderType_i is either 00 or 11