Loading...
Given an array cost where cost[i] is the cost of the ith step, you can start from step 0 or 1. Each step you can climb 1 or 2 steps. Return the minimum cost to reach the top (past the last step).
10,15,20
15
dp where dp[i] represents the minimum cost to reach the ith step. For the given input, cost = [10, 15, 20], we have dp[0] = 10 and dp[1] = 15.dp array by iterating through the cost array. For each step i, we calculate dp[i] = min(dp[i-1], dp[i-2]) + cost[i]. So, dp[2] = min(dp[1], dp[0]) + cost[2] = min(15, 10) + 20 = 10 + 20 = 30.min(dp[n-1], dp[n-2]), where n is the number of steps. In this case, min(dp[2], dp[1]) = min(30, 15) = 15.