Skip to the code
cf.
+10 XP

#107 Sink Empty Grid Slots

easy

A photo grid stores its tiles as numbers in tiles, where 0 is an empty slot. Before rendering, all real tiles should move to the front in their current order, and all empty slots should move to the end.

Write sinkEmpty(tiles) that does this in place, without creating a second array. The tests check tiles after your function runs.

Example 1

Input: tiles = [0,5,0,3,8]
Output: [5,3,8,0,0]

5, 3, 8 keep their order and the two empty slots move to the end.

Example 2

Input: tiles = [1,2,3]
Output: [1,2,3]

There are no empty slots.

Constraints

  • 0 <= tiles.length <= 100000
  • -10^6 <= tiles[i] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).