Trains the technique from
LeetCode 1801Number of Orders in the BacklogThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A produce desk works through instructions in the order logged. Each entry of orders is [price, lots, kind], where kind is 0 for a buy and 1 for a sell.
A buy is filled lot by lot against the cheapest sell waiting on the book, so long as that sell's price is at most the buy's price. A sell is filled against the dearest buy waiting, so long as that buy's price is at least the sell's price. Filling removes lots from both sides, and a waiting order leaves the book once all its lots are gone. Whatever cannot be filled joins the book itself.
Return the total lots left on the book once every instruction has been worked through, modulo 10^9 + 7.
Example 1
The buy at 23 goes on the book first. The sell at 19 fills 4 of its lots, leaving 1 buy lot at 23. The sell at 26 finds no buy dear enough and joins the book. The buy at 21 finds the cheapest sell priced 26, which is too dear, so its 7 lots join too, leaving 1 plus 3 plus 7 lots waiting.
Example 2
The sell wants 11 a lot and the only buy offers 10, so nothing fills and all ten lots stay on the book.
Example 3
An equal price is good enough to fill, so the two orders clear each other out.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def get_number_of_backlog_orders(orders: list[list[int]]) -> int:public int getNumberOfBacklogOrders(int[][] orders)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.