New Dish on the Menu
A café menu lists dishes from cheapest to most expensive. Prices are in cents, so 420 means 4.20, and two dishes can cost the same.
The cook adds a new dish. The printer needs to know which line it goes on so the menu stays sorted. If dishes already have that exact price, the new one goes before them, so it gets seen first.
Your task
Write insertPosition(prices, price). prices is sorted from smallest to largest and may contain repeats. Return the index where price should be inserted: the first index whose price is greater than or equal to price. If every price is smaller, return prices.length.
Do not change the list. An empty menu returns 0. Aim for O(log n).
insertPosition(prices: int[], price: int) → int
Examples
prices = [350, 420, 420, 575, 890] price = 500
3
350, 420 and 420 are cheaper than 500. The first price of 500 or more is 575, at index 3.
prices = [350, 420, 420, 575, 890] price = 420
1
Two dishes already cost 420. The new one goes before both, at index 1. Index 3 would also keep the menu sorted, but the rule says before.
Constraints
- 0 ≤ prices.length ≤ 100,000
- 1 ≤ prices[i], price ≤ 100,000
- prices is sorted in non-decreasing order.
Start with brute force
Walk from the start and return the first index whose price is at least price, or n at the end. That takes O(n) time, which means up to 100,000 checks for each new dish.
A plain binary search that stops at the first equal price is wrong here: in the second example it first lands on index 2, one of the two 420s, and returns 2 instead of 1.
Hints
0 of 4 shown- Hint 1
In
[350, 420, 420, 575, 890], mark each price as "smaller than 500" or "not". What shape does that row of marks have? - Hint 2
Because the list is sorted, it is always a block of "smaller" then a block of "not smaller". You want the first position of the second block.
- Hint 3
Binary search for that boundary, not for a value. If the middle price is smaller than
price, the boundary is to its right. Otherwise the middle might be the boundary, so keep it. - Hint 4
Start with
lo = 0andhi = n(the answer can ben). Whilelo < hi: ifprices[mid] < pricesetlo = mid + 1, elsehi = mid. When they meet, that is the answer.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Search for a boundary, not a value: the first price that is not smaller than the new one.
Trace [350, 420, 420, 575, 890] with price 420:
lo = 0,hi = 5,mid = 2. 420 is not smaller than 420, so index 2 might be the boundary.hi = 2.lo = 0,hi = 2,mid = 1. The price at index 1 is 420 again, sohi = 1.lo = 0,hi = 1,mid = 0. 350 < 420, so the boundary is to the right.lo = 1.lo = hi = 1. Return 1.
Two details matter. hi starts at n, not n - 1, because "after everything" is a valid answer. And when the middle is big enough you set hi = mid, not mid - 1, because the middle might be the answer. The loop still ends, since mid is always less than hi.
function insertPosition(prices, price) {
let lo = 0, hi = prices.length; // the answer is somewhere in [lo, hi]
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (prices[mid] < price) lo = mid + 1; // mid is too cheap: boundary is right of it
else hi = mid; // mid could be the boundary: keep it
}
return lo;
}def insert_position(prices, price):
lo, hi = 0, len(prices) # the answer is somewhere in [lo, hi]
while lo < hi:
mid = (lo + hi) // 2
if prices[mid] < price:
lo = mid + 1 # mid is too cheap: boundary is right of it
else:
hi = mid # mid could be the boundary: keep it
return loRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.