Trains the technique from
LeetCode 283Move ZeroesThis 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.
One column of a spreadsheet is held in the integer array cells. A cell nobody filled in is stored as 0; any other entry is a signed amount somebody typed, which may be negative.
Pack the typed amounts against the front of the column and push every blank behind them. The typed amounts have to come out in the same succession they went in, so only the blanks change position relative to anything else.
Rearrange cells itself using no more than a constant amount of extra space, then return cells.
Example 1
The typed amounts still run 4, then 5, then -3, and the two blanks gather behind them.
Example 2
Four blanks slide back while -6, 3 and -1 hold the order they started in.
Example 3
Nothing is blank here, so the column already satisfies the layout and comes back unchanged.
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 compact_blanks(cells: list[int]) -> list[int]:public int[] compactBlanks(int[] cells)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.