1824. Minimum Sideway Jumps LeetCode Solution
In this guide, you will get 1824. Minimum Sideway Jumps LeetCode Solution with the best time and space complexity. The solution to Minimum Sideway Jumps problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Sideway Jumps solution in C++
- Minimum Sideway Jumps solution in Java
- Minimum Sideway Jumps solution in Python
- Additional Resources

Problem Statement of Minimum Sideway Jumps
There is a 3 lane road of length n that consists of n + 1 points labeled from 0 to n. A frog starts at point 0 in the second lane and wants to jump to point n. However, there could be obstacles along the way.
You are given an array obstacles of length n + 1 where each obstacles[i] (ranging from 0 to 3) describes an obstacle on the lane obstacles[i] at point i. If obstacles[i] == 0, there are no obstacles at point i. 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 i to point i + 1 on the same lane if there is not an obstacle on the lane at point i + 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 2 at point 0.
Note: There will be no obstacles on points 0 and n.
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
1 <= n <= 5 * 105
0 <= obstacles[i] <= 3
obstacles[0] == obstacles[n] == 0
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1824. Minimum Sideway Jumps LeetCode Solution in C++
class Solution {
public:
int minSideJumps(vector<int>& obstacles) {
constexpr int kInf = 1e6;
// dp[i] := the minimum jump to reach the i-th lane
vector<int> dp{kInf, 1, 0, 1};
for (const int obstacle : obstacles) {
if (obstacle > 0)
dp[obstacle] = kInf;
for (int i = 1; i <= 3; ++i) // the current
if (i != obstacle)
for (int j = 1; j <= 3; ++j) // the previous
dp[i] = min({dp[i], dp[j] + (i == j ? 0 : 1)});
}
return ranges::min(dp);
}
};
/* code provided by PROGIEZ */
1824. Minimum Sideway Jumps LeetCode Solution in Java
class Solution {
public int minSideJumps(int[] obstacles) {
final int kInf = (int) 1e6;
// dp[i] := the minimum jump to reach the i-th lane
int[] dp = {kInf, 1, 0, 1};
for (final int obstacle : obstacles) {
if (obstacle > 0)
dp[obstacle] = kInf;
for (int i = 1; i <= 3; ++i) // the current
if (i != obstacle)
for (int j = 1; j <= 3; ++j) // the previous
dp[i] = Math.min(dp[i], dp[j] + (i == j ? 0 : 1));
}
return Arrays.stream(dp).min().getAsInt();
}
}
// code provided by PROGIEZ
1824. Minimum Sideway Jumps LeetCode Solution in Python
class Solution:
def minSideJumps(self, obstacles: list[int]) -> int:
kInf = 1e6
# dp[i] := the minimum jump to reach the i-th lane
dp = [kInf, 1, 0, 1]
for obstacle in obstacles:
print(dp)
if obstacle > 0:
dp[obstacle] = kInf
for i in range(1, 4): # the current
if i != obstacle:
for j in range(1, 4): # the previous
dp[i] = min(dp[i], dp[j] + (0 if i == j else 1))
return min(dp)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.