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.i
from 1 to n
.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
.dp[i][j]
to be the minimum of its current value and dp[i-1][j]
(no side jump required).j
, if there is an obstacle at point i
on lane j
, update dp[i][j]
to be infinity.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).dp
array, the answer is the minimum value in dp[n]
.dp[n]
as the final answer.