Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
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.
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 + 11 <= n <= 5 * 1050 <= obstacles[i] <= 3obstacles[0] == obstacles[n] == 0Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy
[0,1,2,3,0]
[0,1,1,3,3,0]
[0,2,1,0,3,0]
frog-jump)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1824: Minimum Sideway Jumps
class Solution {
public int minSideJumps(int[] obstacles) {
final int inf = 1 << 30;
int[] f = {1, 0, 1};
for (int i = 1; i < obstacles.length; ++i) {
for (int j = 0; j < 3; ++j) {
if (obstacles[i] == j + 1) {
f[j] = inf;
break;
}
}
int x = Math.min(f[0], Math.min(f[1], f[2])) + 1;
for (int j = 0; j < 3; ++j) {
if (obstacles[i] != j + 1) {
f[j] = Math.min(f[j], x);
}
}
}
return Math.min(f[0], Math.min(f[1], f[2]));
}
}
// Accepted solution for LeetCode #1824: Minimum Sideway Jumps
func minSideJumps(obstacles []int) int {
f := [3]int{1, 0, 1}
const inf = 1 << 30
for _, v := range obstacles[1:] {
for j := 0; j < 3; j++ {
if v == j+1 {
f[j] = inf
break
}
}
x := min(f[0], min(f[1], f[2])) + 1
for j := 0; j < 3; j++ {
if v != j+1 {
f[j] = min(f[j], x)
}
}
}
return min(f[0], min(f[1], f[2]))
}
# Accepted solution for LeetCode #1824: Minimum Sideway Jumps
class Solution:
def minSideJumps(self, obstacles: List[int]) -> int:
f = [1, 0, 1]
for v in obstacles[1:]:
for j in range(3):
if v == j + 1:
f[j] = inf
break
x = min(f) + 1
for j in range(3):
if v != j + 1:
f[j] = min(f[j], x)
return min(f)
// Accepted solution for LeetCode #1824: Minimum Sideway Jumps
struct Solution;
impl Solution {
fn min_side_jumps(obstacles: Vec<i32>) -> i32 {
let n = obstacles.len();
let mut dp: Vec<Vec<i32>> = vec![vec![0, 0, 0]; n];
dp[0][0] = 1;
dp[0][2] = 1;
for i in 1..n {
for j in 0..3 {
if obstacles[i] == (j + 1) as i32 {
dp[i][j] = std::i32::MAX;
} else {
let mut min = std::i32::MAX;
for k in 0..3 {
if !(obstacles[i - 1] == (k + 1) as i32 || obstacles[i] == (k + 1) as i32) {
min = min.min(dp[i - 1][k] + if k == j { 0 } else { 1 });
}
}
dp[i][j] = min;
}
}
}
*dp[n - 1].iter().min().unwrap()
}
}
#[test]
fn test() {
let obstacles = vec![0, 1, 2, 3, 0];
let res = 2;
assert_eq!(Solution::min_side_jumps(obstacles), res);
let obstacles = vec![0, 1, 1, 3, 3, 0];
let res = 0;
assert_eq!(Solution::min_side_jumps(obstacles), res);
let obstacles = vec![0, 2, 1, 0, 3, 0];
let res = 2;
assert_eq!(Solution::min_side_jumps(obstacles), res);
}
// Accepted solution for LeetCode #1824: Minimum Sideway Jumps
function minSideJumps(obstacles: number[]): number {
const inf = 1 << 30;
const f = [1, 0, 1];
for (let i = 1; i < obstacles.length; ++i) {
for (let j = 0; j < 3; ++j) {
if (obstacles[i] == j + 1) {
f[j] = inf;
break;
}
}
const x = Math.min(...f) + 1;
for (let j = 0; j < 3; ++j) {
if (obstacles[i] != j + 1) {
f[j] = Math.min(f[j], x);
}
}
}
return Math.min(...f);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.