Order book budget limit
Your task
A trading screen draws the sell side of an order book as a ladder of rungs. ask_cents holds the resting ask prices in cents, sorted ascending, and several orders can rest on one rung, so equal prices repeat. A trader sets a per unit limit of budget_cents and wants to know how far down that ladder the limit reaches. Given ask_cents and budget_cents, return the index of the last rung priced at or below the budget. When a run of rungs shares that price, return the last of them, since every rung in the run is affordable. When even the cheapest rung costs more than the budget, return -1, and an empty ladder returns -1 too. Prices are positive whole cents and the ladder is redrawn on every tick, so the lookup has to be logarithmic.
Examples
last_affordable_rung(ask_cents: list[int], budget_cents: int) -> intExample 1
Input
last_affordable_rung([100, 250, 250, 400], 250)Output
2Example 2
Input
last_affordable_rung([100, 250, 400], 99)Output
-1Example 3
Input
last_affordable_rung([100, 250, 400], 1000)Output
2