Leetcode 1824: Minimum Sideway Jumps

grid47
grid47
Exploring patterns and algorithms
May 8, 2024 8 min read

A frog is traveling on a 3-lane road, starting at point 0 on lane 2. The frog wants to reach point n, but there may be obstacles at various points on the road. The frog can either move forward on the same lane or jump to another lane if the current lane is blocked. The goal is to find the minimum number of side jumps the frog needs to take to reach point n while avoiding obstacles.
Problem
Approach
Steps
Complexity
Input: You are given an array obstacles where obstacles[i] (0 <= i <= n) represents the obstacle at point i in lane obstacles[i]. The value of obstacles[i] can be 0 (no obstacle), 1 (obstacle on lane 1), 2 (obstacle on lane 2), or 3 (obstacle on lane 3).
Example: For example, obstacles = [0, 1, 2, 3, 0] means that there are obstacles on lane 1 at point 1, lane 2 at point 2, and lane 3 at point 3.
Constraints:
• 1 <= n <= 5 * 10^5
• 0 <= obstacles[i] <= 3
• obstacles[0] == obstacles[n] == 0
Output: Return the minimum number of side jumps the frog needs to take to reach point n, starting from point 0 on lane 2.
Example: For obstacles = [0, 1, 2, 3, 0], the frog needs 2 side jumps to avoid obstacles and reach point n, so the output should be 2.
Constraints:
• The output should be a non-negative integer.
Goal: The frog needs to avoid obstacles and reach point n with the fewest side jumps.
Steps:
• 1. Start at point 0, lane 2.
• 2. At each point, check if there is an obstacle in the current lane.
• 3. If there is an obstacle in the current lane, consider jumping to the other two lanes if they are clear of obstacles.
• 4. Continue this process until the frog reaches point n, keeping track of the minimum number of jumps.
Goal: The frog must avoid obstacles and make side jumps only when necessary.
Steps:
• The frog starts at point 0, lane 2, and must reach point n.
• At each point, there can be at most one obstacle in the three lanes.
• The frog can jump to any lane as long as the new lane does not have an obstacle at the current point.
Assumptions:
• The frog can always make a side jump if needed, as long as the destination lane is clear of obstacles.
• The frog will only need to jump when its current lane is blocked.
Input: Input: obstacles = [0, 1, 2, 3, 0]
Explanation: The frog starts on lane 2 at point 0. It faces an obstacle on lane 1 at point 1, so it jumps to lane 3. It then faces an obstacle on lane 3 at point 2, so it jumps to lane 1. After jumping twice, the frog reaches point n, having made 2 side jumps.

Input: Input: obstacles = [0, 1, 1, 3, 3, 0]
Explanation: The frog starts on lane 2 at point 0. There are no obstacles on lane 2 at point 1, so it continues straight ahead. No side jumps are needed, and the frog reaches point n without any obstacles.

Input: Input: obstacles = [0, 2, 1, 0, 3, 0]
Explanation: The frog faces an obstacle on lane 2 at point 1, so it jumps to lane 3. It then faces an obstacle on lane 3 at point 3, so it jumps to lane 1. The frog makes 2 side jumps to reach point n.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus