jayati

Min Cost Climbing Stairs

Sep 25th, 2022 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. //Used Dynamic Programming but narrowed it down to 3rd approach i.e tabulation for better time and space complexity of O(n)
  2. class Solution {
  3.    
  4. public:
  5.     int minCostClimbingStairs(vector<int>& cost) {
  6.         int n=cost.size();
  7.         int prev1=cost[0];
  8.         int prev2=cost[1];
  9.  
  10.         int ans;
  11.         for(int i=2;i<n;i++)
  12.         {
  13.             int curr=cost[i]+min(prev1,prev2);
  14.             prev1=prev2;
  15.             prev2=curr;
  16.         }
  17.         return min(prev1,prev2);
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment