All problems
1144MediumArrayDynamic ProgrammingGreedy

Sideways Jumps Down the Three-Track Chute

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1824Minimum Sideway Jumps

This 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.

Three tracks numbered 1, 2 and 3 run side by side from point 0 to the last point of a chute. blocks[i] names the one track shut at point i, or is 0 when no track is shut there.

A trolley starts at point 0 on track 2 and rolls forward to the last point. It cannot roll onto a point where its own track is shut.

At any point the trolley may jump sideways to either of the other two tracks, so long as that track is not shut at that point. A jump from track 1 straight to track 3 counts as one jump. Rolling forward costs nothing.

Return the fewest jumps needed to reach the last point on any track. A way through always exists.

Examples

Example 1

Input
blocks = [0, 2, 0]
Output
1

Track 2 is shut at the middle point, so a single jump onto either other track carries the trolley through.

Example 2

Input
blocks = [0, 2, 2, 2, 2, 2, 0]
Output
1

Track 2 is shut at every point along the way, so one jump at the start onto another track is enough and nothing further is needed.

Example 3

Input
blocks = [0, 3, 2, 1, 0]
Output
2

Track 3 is shut at the first point so the trolley jumps to track 1. Track 1 is shut at the third point and track 2 is shut where the trolley stands, so the second jump has to go straight across from track 1 to track 3.

Constraints

  • 2 <= blocks.length <= 500001
  • 0 <= blocks[i] <= 3
  • blocks[0] == 0
  • the last entry of blocks is 0

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_side_jumps(blocks: list[int]) -> int:
Java
public int minSideJumps(int[] blocks)
September 7
Apply