Question: 3. (12 pts) Consider a staircase with n steps. Each step i has some non-negative cost, cost[i]. Steps are indexed from 0 to n-1. You

3. (12 pts) Consider a staircase with n steps. Each step i has some non-negative cost, cost[i]. Steps are indexed from 0 to n-1. You want to climb these stairs. Once you pay the cost, you can either climb one or two steps. Design a dynamic programming algorithm to find the minimum cost to reach the top of the floor. You can either start from the step or step 1. Some examples are as follows: Example 1: input: cost = (10,15,20) Output = 15 (minimum cost is obtained when you start on cost[1], pay that cost and go to top) Example 2: input: cost = (1,100,1,1,1,100,1,1,100,1) Output = 6 (minimum cost is obtained when you start on cost[O], and only step on 1s, skipping cost[3]) Hint: Define f[i] as the final cost to climb to the top from step i. Find a recurrence relation for f[i]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
