Trains the technique from
LeetCode 773Sliding PuzzleThis 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.
A tray holds five marked tiles and one gap, laid out as tray, two rows of three. The tiles carry the marks 1 through 5 and the gap is written 0.
One slide moves a tile that sits directly beside the gap, edgewise, into the gap; the tile and the gap change places.
Return the fewest slides needed to reach the layout with 1, 2, 3 along the top row and 4, 5 then the gap along the bottom, or -1 when no number of slides can reach it.
Example 1
The tray is already in the wanted layout, so nothing needs sliding.
Example 2
Sliding the 4 up, then the 5 left, then the 3 left brings the tray into order in three slides.
Example 3
Only the 1 and the 2 are out of order, with everything else already in place. A slide never changes the layout's parity, so this layout can never reach the wanted one.
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 sliding_puzzle(tray: list[list[int]]) -> int:public int slidingPuzzle(int[][] tray)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.