LeetCode 1801: Number of Orders in the Backlog

LeetCode 1801 Solution Explanation

Explanation

To solve this problem, we can use a priority queue for both buy and sell orders. We iterate through the orders and process them accordingly. For each buy order, we match it with the smallest sell order in the sell backlog that has a price less than or equal to the current buy order's price. Similarly, for each sell order, we match it with the largest buy order in the buy backlog that has a price greater than or equal to the current sell order's price.

We maintain two priority queues, one for buy orders and one for sell orders. We process each order and update the backlogs accordingly. Finally, we return the total number of orders left in the backlog modulo 10^9 + 7.

Time Complexity:

  • The time complexity of this approach is O(n log n) where n is the number of orders.

Space Complexity:

  • The space complexity is O(n) to store the orders in the priority queues.

LeetCode 1801 Solutions in Java, C++, Python

import java.util.PriorityQueue;

class Solution {
    public int getNumberOfBacklogOrders(int[][] orders) {
        long MOD = 1000000007;
        PriorityQueue<int[]> buy = new PriorityQueue<>((a, b) -> b[0] - a[0]); // Max heap for buy orders
        PriorityQueue<int[]> sell = new PriorityQueue<>((a, b) -> a[0] - b[0]); // Min heap for sell orders
        
        for (int[] order : orders) {
            if (order[2] == 0) { // Buy order
                while (!sell.isEmpty() && sell.peek()[0] <= order[0] && order[1] > 0) {
                    int[] currSell = sell.poll();
                    int minAmount = Math.min(currSell[1], order[1]);
                    order[1] -= minAmount;
                    currSell[1] -= minAmount;
                    if (currSell[1] > 0) {
                        sell.offer(currSell);
                    }
                }
                if (order[1] > 0) {
                    buy.offer(order);
                }
            } else { // Sell order
                while (!buy.isEmpty() && buy.peek()[0] >= order[0] && order[1] > 0) {
                    int[] currBuy = buy.poll();
                    int minAmount = Math.min(currBuy[1], order[1]);
                    order[1] -= minAmount;
                    currBuy[1] -= minAmount;
                    if (currBuy[1] > 0) {
                        buy.offer(currBuy);
                    }
                }
                if (order[1] > 0) {
                    sell.offer(order);
                }
            }
        }
        
        long total = 0;
        while (!buy.isEmpty()) {
            total += buy.poll()[1];
        }
        while (!sell.isEmpty()) {
            total += sell.poll()[1];
        }
        
        return (int) (total % MOD);
    }
}

Interactive Code Editor for LeetCode 1801

Improve Your LeetCode 1801 Solution

Use the editor below to refine the provided solution for LeetCode 1801. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems