Class Solution
-
- All Implemented Interfaces:
public final class Solution1824 - Minimum Sideway Jumps.
Medium
There is a 3 lane road of length
nthat consists ofn + 1points labeled from0ton. A frog starts at point0in the second lane and wants to jump to pointn. However, there could be obstacles along the way.You are given an array
obstaclesof lengthn + 1where eachobstacles[i]( ranging from 0 to 3 ) describes an obstacle on the laneobstacles[i]at pointi. Ifobstacles[i] == 0, there are no obstacles at pointi. There will be at most one obstacle in the 3 lanes at each point.For example, if
obstacles[2] == 1, then there is an obstacle on lane 1 at point 2.
The frog can only travel from point
ito pointi + 1on the same lane if there is not an obstacle on the lane at pointi + 1. To avoid obstacles, the frog can also perform a side jump to jump to another lane (even if they are not adjacent) at the same point if there is no obstacle on the new lane.For example, the frog can jump from lane 3 at point 3 to lane 1 at point 3.
Return the minimum number of side jumps the frog needs to reach any lane at point n starting from lane
2at point 0.Note: There will be no obstacles on points
0andn.Example 1:
Input: obstacles = 0,1,2,3,0
Output: 2
Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps (red arrows). Note that the frog can jump over obstacles only when making side jumps (as shown at point 2).
Example 2:
Input: obstacles = 0,1,1,3,3,0
Output: 0
Explanation: There are no obstacles on lane 2. No side jumps are required.
Example 3:
Input: obstacles = 0,2,1,0,3,0
Output: 2
Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps.
Constraints:
obstacles.length == n + 1<code>1 <= n <= 5 * 10<sup>5</sup></code>
0 <= obstacles[i] <= 3obstacles[0] == obstacles[n] == 0
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminSideJumps(IntArray obstacles)-
-
Method Detail
-
minSideJumps
final Integer minSideJumps(IntArray obstacles)
-
-
-
-