diff --git a/Leetcode/Min_Cost_Climbing_Stairs.cpp b/Leetcode/Min_Cost_Climbing_Stairs.cpp index c7ffc5b..84a85c6 100644 --- a/Leetcode/Min_Cost_Climbing_Stairs.cpp +++ b/Leetcode/Min_Cost_Climbing_Stairs.cpp @@ -1,13 +1,41 @@ class Solution { public: - int minCostClimbingStairs(vector& cost) { - int n=cost.size(); - int dp[n]; - dp[0]=cost[0]; - dp[1]=cost[1]; - for(int i=2;i &cost) + { +// this recurrence gives us the minimum cost of reaching the top of the floor + +// pruning + if(level > size) + { + return 1001; +// we will return anything > 999 so that this partial-solution is never chosen + } +// base case + + if(level == size) + { + return 0; + } + + if(dp[level] != -1) + { + return dp[level]; } - return min(dp[n-1],dp[n-2]); + +// choices +// choices will be to either take 1 step or two steps + + int ans = 0; + + ans = cost[level] + min(rec(level + 1, size, cost), rec(level + 2, size, cost)); + +// return and save + dp[level] = ans; + return ans; + } + int minCostClimbingStairs(vector& cost) { + memset(dp, -1, sizeof(dp)); + return min(rec(1, cost.size(), cost), rec(0, cost.size(), cost)); } -}; +}; \ No newline at end of file