Skip to content

Commit

Permalink
Merge pull request Rohit0301#63 from Jay179-sudo/Climb-Stairs
Browse files Browse the repository at this point in the history
Added working code solution for the Leetcode Problem 'Min Cost Climbi…
  • Loading branch information
Rohit0301 authored Oct 3, 2022
2 parents d4c159c + 19a5fc0 commit cf33a2c
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions Leetcode/Min_Cost_Climbing_Stairs.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n=cost.size();
int dp[n];
dp[0]=cost[0];
dp[1]=cost[1];
for(int i=2;i<n;i++){
dp[i]=min(dp[i-1],dp[i-2])+cost[i];
int dp[1000];
int rec(int level, int size, vector<int> &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<int>& cost) {
memset(dp, -1, sizeof(dp));
return min(rec(1, cost.size(), cost), rec(0, cost.size(), cost));
}
};
};

0 comments on commit cf33a2c

Please sign in to comment.