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
Minimum Sideway Jumps INPUT 3-Lane Highway (Point 0 to 4) Lane 1 Lane 2 Lane 3 0 1 2 3 4 X X X F obstacles array: 0 1 2 3 0 [0] [1] [2] [3] [4] 0 = no obstacle 1,2,3 = obstacle in lane Frog starts at Lane 2, Point 0 ALGORITHM STEPS 1 Initialize DP dp[lane] = min jumps to reach Lane 2: 0, Others: 1 2 Process Each Point For each point 0 to n: Check obstacles, update costs 3 Handle Jumps If obstacle ahead: jump +1 Update min cost for each lane 4 Return Minimum min(dp[1], dp[2], dp[3]) DP State Tracking: Lane pt0 pt1 pt2 pt4 L1 1 X 1 2 L2 0 0 X 2 L3 1 1 1 2 FINAL RESULT Optimal Path Taken: X X X F J1 J2 E 0 1 2 3 4 Forward move (free) Sideway jump (cost 1) Output: 2 Minimum sideway jumps = 2 Jump at point 1: L2 --> L3 Jump at point 3: L3 --> L1 Key Insight: Use Dynamic Programming with BFS-like updates. Track minimum jumps to reach each lane at each point. When an obstacle blocks a lane, the frog must jump sideways (+1 cost) to any available adjacent lane. Time: O(n), Space: O(1) - only need to track 3 lane states at each position. TutorialsPoint - Minimum Sideway Jumps | Optimal Solution (Dynamic Programming)
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