Sep 21, 2024
n
stairs.i
to stair j
is given by:energy = height[i] - height[j]
(i+1)
th stair or the (i+2)
th stair.f(n)
be the minimum energy required to reach the nth stair.f(0) = 0
(cost to stay on the first stair is zero).f(i) = min(f(i-1) + abs(height[i] - height[i-1]), f(i-2) + abs(height[i] - height[i-2]))
f(i-2)
if i > 1
.n+1
.dp[i] = min(dp[i], computed_result)
.O(n)
due to linear recursion calls and storing results.dp[0] = 0
.prev1
and prev2
to calculate the minimum energy iteratively.k
stairs (i.e., i + 1
to i + k
).