Order matching engine
Your task
A matching engine keeps resting orders on two sides. Each entry of orders is 'B 105 4' or 'S 99 6': the side, a price in cents, and a quantity of at least 1. An arriving buy trades against the cheapest resting sell priced at or below it, and an arriving sell trades against the dearest resting buy priced at or above it, repeating while a trade is possible. Orders resting at the same price are taken oldest first. Each trade happens at the resting order's price and moves the smaller of the two open quantities. Whatever is left of the arriver rests on its own side. Return, for each arriving order in turn, the total cash in cents that its trades moved.
Examples
order_book_fills(orders: list[str]) -> list[int]Example 1
Input
order_book_fills(["B 100 5", "S 98 3", "S 101 4"])Output
[0, 300, 0]Example 2
Input
order_book_fills(["S 20 1", "S 22 1", "S 21 1", "B 25 3"])Output
[0, 0, 0, 63]Example 3
Input
order_book_fills(["B 30 10", "S 30 4", "S 30 4", "S 30 4"])Output
[0, 120, 120, 60]