Minimum Sideway Jumps - Problem

Imagine a 3-lane highway stretching from point 0 to point n, where a clever frog needs to navigate from the starting position to the end while avoiding obstacles!

๐Ÿธ The Setup: Our frog begins at point 0 in lane 2 (the middle lane) and wants to reach point n. The road has obstacles scattered along the way - but here's the catch: the frog can jump sideways to any other lane at the same point to avoid obstacles!

The Rules:

  • The frog moves forward one point at a time in the same lane
  • If there's an obstacle ahead, the frog can perform a sideways jump to switch lanes
  • Sideways jumps can go to any available lane (even non-adjacent ones!)
  • Each sideways jump costs 1 point

Your Mission: Find the minimum number of sideways jumps needed to reach the destination!

obstacles[i] tells you which lane (1, 2, or 3) has an obstacle at point i, or 0 if no obstacles exist at that point.

Input & Output

example_1.py โ€” Basic Path Navigation
$ Input: obstacles = [0,1,2,3,0]
โ€บ Output: 2
๐Ÿ’ก Note: The frog starts at lane 2. At point 1 there's an obstacle in lane 1, at point 2 obstacle in lane 2, at point 3 obstacle in lane 3. Optimal path: Stay in lane 2 โ†’ Jump to lane 3 (1st jump) โ†’ Jump to lane 2 (2nd jump) โ†’ Reach end. Total: 2 jumps.
example_2.py โ€” Minimal Jumps
$ Input: obstacles = [0,1,1,3,3,0]
โ€บ Output: 0
๐Ÿ’ก Note: The frog can stay in lane 2 throughout the entire journey without any sideways jumps, as lane 2 never has obstacles in this configuration.
example_3.py โ€” Strategic Planning
$ Input: obstacles = [0,2,1,0,3,0]
โ€บ Output: 2
๐Ÿ’ก Note: Starting in lane 2, there's an obstacle at point 1. Jump to lane 1 or 3 (1st jump). At point 2, obstacle in lane 1, so if we were in lane 1, jump to lane 2 or 3 (2nd jump). Optimal strategy requires 2 total jumps.

Constraints

  • obstacles.length == n + 1
  • 1 โ‰ค n โ‰ค 5 ร— 105
  • 0 โ‰ค obstacles[i] โ‰ค 3
  • obstacles[0] == obstacles[n] == 0 (no obstacles at start and end)

Visualization

Tap to expand
L1L2L3๐Ÿธ๐Ÿšง๐Ÿšง๐ŸšงJump 1Jump 2๐ŸStart
Point 0Point 1Point 2Point 3End
Point 4
DP State Trackingโ€ข Track min jumps to reach each laneโ€ข Consider stay vs jump at each point๐Ÿธ Minimum Sideways Jumps = 2
Understanding the Visualization
1
Starting Position
Frog begins at point 0 in lane 2 (middle lane)
2
Obstacle Detection
At each point, check for obstacles in the next position
3
Decision Making
Choose to stay in current lane or jump to avoid obstacles
4
Optimal Path
DP ensures we always take the path with minimum jumps
Key Takeaway
๐ŸŽฏ Key Insight: Dynamic Programming tracks the minimum jumps needed to reach each lane at every position, making locally optimal decisions that lead to a globally optimal solution.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~15 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen