Number of Orders in the Backlog - Complete Solution Guide
Number of Orders in the Backlog is LeetCode problem 1801, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 2D integer array orders , where each orders[i] = [price i , amount i , orderType i ] denotes that amount i orders have been placed of type orderType i at the price price i . The orderType i 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 amount i 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
Detailed Explanation
The problem simulates an order book where buy and sell orders are matched based on price. Given a series of orders, each with a price, amount, and type (buy or sell), the goal is to determine the total amount of orders remaining in the backlog after processing all orders, modulo 10^9 + 7. The backlog consists of unmatched buy and sell orders. Buy orders are matched with the lowest-priced sell orders, and sell orders are matched with the highest-priced buy orders. Orders are processed in the order they are given in the input array.
Solution Approach
The solution uses two priority queues, `sell_backlog` (min-heap) and `buy_backlog` (max-heap), to simulate the order book. It iterates through the given `orders` array. For each order, it checks if it's a buy or sell order. If it's a buy order, it tries to match it with the lowest-priced sell orders in `sell_backlog`. If it's a sell order, it tries to match it with the highest-priced buy orders in `buy_backlog`. If there are remaining amounts after matching, the remaining order is added to its respective backlog. Finally, the total amount of orders in both backlogs is calculated and returned modulo 10^9 + 7.
Step-by-Step Algorithm
- Step 1: Initialize a min-heap (`sell_backlog`) to store sell orders (price, amount) and a max-heap (`buy_backlog`) to store buy orders (price, amount).
- Step 2: Iterate through the input `orders` array.
- Step 3: For each order, extract the price, amount, and order type.
- Step 4: If the order is a buy order (order_type == 0):
- Step 5: While there are sell orders in the `sell_backlog`, the current buy amount is greater than 0, and the lowest sell price is less than or equal to the buy price, match the orders:
- Step 6: Pop the lowest sell order from `sell_backlog`.
- Step 7: If the sell amount is less than or equal to the buy amount, reduce the buy amount by the sell amount.
- Step 8: Otherwise, reduce the sell amount by the buy amount and push the remaining sell order back into `sell_backlog`, then set the buy amount to 0.
- Step 9: If there's any remaining buy amount, push the buy order into `buy_backlog`.
- Step 10: If the order is a sell order (order_type == 1):
- Step 11: While there are buy orders in the `buy_backlog`, the current sell amount is greater than 0, and the highest buy price is greater than or equal to the sell price, match the orders:
- Step 12: Pop the highest buy order from `buy_backlog`.
- Step 13: If the buy amount is less than or equal to the sell amount, reduce the sell amount by the buy amount.
- Step 14: Otherwise, reduce the buy amount by the sell amount and push the remaining buy order back into `buy_backlog`, then set the sell amount to 0.
- Step 15: If there's any remaining sell amount, push the sell order into `sell_backlog`.
- Step 16: After processing all orders, calculate the total amount of orders in both `sell_backlog` and `buy_backlog`.
- Step 17: Return the total amount modulo 10^9 + 7.
Key Insights
- Insight 1: The key insight is to use priority queues (heaps) to efficiently maintain the buy and sell order backlogs, allowing for quick access to the minimum sell price and maximum buy price.
- Insight 2: Using a min-heap for sell orders and a max-heap for buy orders ensures that the best matching orders are always at the top of the heaps.
- Insight 3: The modulo operation (10^9 + 7) is crucial to prevent integer overflow because the amount of orders can be very large (up to 10^9 per order).
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Heap (Priority Queue), Simulation.
Companies
Asked at: Coinbase, Jane Street, Robinhood.