Leetcode Problem 1824. Minimum Sideway Jumps

1824. Minimum Sideway Jumps

Leetcode Solutions

Dynamic Programming Approach

  1. Initialize a 2D array dp of size n+1 by 3 with infinity, except for dp[0][1] which is set to 0 since the frog starts at lane 2 (1-indexed) with no side jumps.
  2. Iterate through each point i from 1 to n.
  3. For each lane j from 1 to 3, check if there is no obstacle at point i on lane j and at point i-1 on lane j.
  4. If there is no obstacle, update dp[i][j] to be the minimum of its current value and dp[i-1][j] (no side jump required).
  5. For each lane j, if there is an obstacle at point i on lane j, update dp[i][j] to be infinity.
  6. For each lane j, update dp[i][j] to be the minimum of its current value and the minimum side jumps required to reach any of the other two lanes at point i-1 plus one (for the side jump).
  7. After filling the dp array, the answer is the minimum value in dp[n].
  8. Return the minimum value in dp[n] as the final answer.
UML Thumbnail

Greedy Approach with Jump Simulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...