Bakery Tray Gaps
A bakery's display tray is one row of slots. Each holds a pastry, written as its product number, or is empty, written as 0. Morning sales leave gaps all over it.
Before lunch, the baker slides every pastry to the front so the gaps collect at the back for the new batch. The pastries keep their order, because the labels on the glass follow it.
Your task
Write compactTray(tray). Rearrange tray in place so all non-zero numbers come first, in their original order, followed by all the zeros. Return nothing.
Do not create a second list. The length of tray stays the same.
compactTray(tray: int[]) → void (changes tray in place)
Examples
tray = [0, 41, 0, 7, 12]
[41, 7, 12, 0, 0]
The pastries 41, 7, 12 move to the front in that order. The two gaps end up at the back.
tray = [9, 3, 0, 0, 5]
[9, 3, 5, 0, 0]
9 and 3 are already in place; only 5 moves forward past the two gaps. Sorting would be wrong: the order 9, 3, 5 must be kept.
Constraints
- 0 ≤ tray.length ≤ 10,000
- 0 ≤ tray[i] ≤ 999
Start with brute force
Each time you find a gap, delete it and add a 0 at the end. Or shift everything after the first gap one place left, again and again.
Both are correct, but every delete or shift moves all later slots: on a 10,000-slot tray that is half gaps, about 5,000 shifts of up to 10,000 slots. Each pastry only needs to move once, straight to its final slot.
Hints
0 of 4 shown- Hint 1
In
[0, 41, 0, 7, 12], which slot should 41 end up in? And 7? Can you tell just by counting the pastries already seen? - Hint 2
The k-th pastry from the left always ends up in slot k-1. So while scanning, you always know the next slot to fill.
- Hint 3
Use two indexes: one scans every slot, the other points at the next slot to fill and moves only when a pastry is placed.
- Hint 4
Scan with
rand keep a second indexw, the next slot to fill, starting at 0. Whentray[r]is not 0, swaptray[w]andtray[r], then add 1 tow. The swap moves the pastry forward and a gap backward at once.
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
The first pastry you meet goes to slot 0, the second to slot 1, and so on. Keep a write index w for the next slot to fill, and scan every slot with a read index r.
Trace [0, 41, 0, 7, 12]:
r= 0 is a gap. Do nothing.wstays 0.r= 1 is 41. Swap slots 0 and 1:[41, 0, 0, 7, 12].w= 1.r= 2 is a gap. Do nothing.r= 3 is 7. Swap slots 1 and 3:[41, 7, 0, 0, 12].w= 2.r= 4 is 12. Swap slots 2 and 4:[41, 7, 12, 0, 0].w= 3.
Everything between w and r is a gap, so each swap moves a pastry forward and a gap back. Pastries are placed in the order met, so their order is kept.
function compactTray(tray) {
let w = 0; // next slot that should get a pastry
for (let r = 0; r < tray.length; r++) {
if (tray[r] !== 0) {
[tray[w], tray[r]] = [tray[r], tray[w]];
w++;
}
}
}def compact_tray(tray):
w = 0 # next slot that should get a pastry
for r in range(len(tray)):
if tray[r] != 0:
tray[w], tray[r] = tray[r], tray[w]
w += 1Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.