Min Cost Climbing Stairs
ID: 746; easy
Last updated
ID: 746; easy
Last updated
This is the DP equation:
For the minimum cost to reach the ith step, we either came from the one-step hop or from the two-step hop (with their corresponding minimum costs as well).
Time complexity: O(n)
Space complexity: O(n)
Similarly, we can improve the space complexity by just using variables instead of the whole array. It become a little more abstract now.
downOne
and downTwo
represents the minimum cost to reach one step and two steps below the current step that we are taking. Inside the loop, each iteration, downOne
is after downTwo
. We calculate the minimum and update downOne
. Then, downTwo
is updated to be the old downOne
.