Tap here to switch tabs
Problem
Submissions

Problem: Minimum Sideway Jumps

med
30 min
Try to solve the Minimum Sideway Jumps problem.

Statement

There is a 33 lane road of length nn that consists of n+1n + 1 points labeled from 00 to nn. A frog starts at point 00 on lane 22 and aims to reach point nn on any lane. There may be obstacles along the road.

You are given an integer array obstacles of length n+1n + 1, where obstacles[i] indicates that there is an obstacle on lane obstacles[i] at point i. If obstacles[i] is 00, there are no obstacles at point i. There is at most one obstacle among the 33 lanes at any given point.

When the frog is on lane lane at point i:

  • The frog can move straight ahead to point i+1i + 1 on the same lane lane if there is no obstacle on lane lane at point i+1i + 1.

  • The frog can make a side jump to any other lane at the same point i if the chosen target lane does not have an obstacle at point i.

Return the minimum number of side jumps required for the frog to reach point nn starting from lane 22 at point 00.

Note: The frog cannot jump to a lane that contains an obstacle at the current point, but it can jump across an obstructed lane.

Constraints:

  • obstacles.length =n+1= n + 1

  • 1≤n≤5imes1051 \leq n \leq 5 imes 10^5

  • 0≤0 \leq obstacles[i] ≤3\leq 3

  • obstacles[0] =0= 0 and obstacles[n] =0= 0

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Sideway Jumps

med
30 min
Try to solve the Minimum Sideway Jumps problem.

Statement

There is a 33 lane road of length nn that consists of n+1n + 1 points labeled from 00 to nn. A frog starts at point 00 on lane 22 and aims to reach point nn on any lane. There may be obstacles along the road.

You are given an integer array obstacles of length n+1n + 1, where obstacles[i] indicates that there is an obstacle on lane obstacles[i] at point i. If obstacles[i] is 00, there are no obstacles at point i. There is at most one obstacle among the 33 lanes at any given point.

When the frog is on lane lane at point i:

  • The frog can move straight ahead to point i+1i + 1 on the same lane lane if there is no obstacle on lane lane at point i+1i + 1.

  • The frog can make a side jump to any other lane at the same point i if the chosen target lane does not have an obstacle at point i.

Return the minimum number of side jumps required for the frog to reach point nn starting from lane 22 at point 00.

Note: The frog cannot jump to a lane that contains an obstacle at the current point, but it can jump across an obstructed lane.

Constraints:

  • obstacles.length =n+1= n + 1

  • 1≤n≤5imes1051 \leq n \leq 5 imes 10^5

  • 0≤0 \leq obstacles[i] ≤3\leq 3

  • obstacles[0] =0= 0 and obstacles[n] =0= 0