Min Cost Climbing Stairs | Practice | GeeksforGeeks

Given an array of integers cost[] of length N, where cost[i] is the cost of the ith step on a staircase. Once the cost is paid, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.

Example 1:
Input:
N = 3
cost[] = {10, 15, 20}
Output:
15
Explanation:

Cheapest option is to start at cost[1],
pay that cost, and go to the top.

 

Example 2:
Input:
N = 10
arr[] = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}
Output:
6
Explanation:
Cheapest option is to start on cost[0], 
and only step on 1s, skipping cost[3].


Your Task:
The task is to complete the function minCostClimbingStairs() which takes an array cost[] as input and returns the minimum cost to reach the top.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
2 ≤ N ≤ 103
0 ≤ cost[i] ≤ 999
 
0
devashishbakare2 weeks ago
JAVA Solution using DP(Tabulation) with explanation
static int minCostClimbingStairs(int[] cost , int n) {
        //created this array to store the mininum jump we make to reach at destination with less cost
        int dp[] = new int[n];
        
        //add the last 2 steps ans into in dp array for memorisation
        dp[n-1] = cost[n-1];
        dp[n-2] = cost[n-2];
        
        //start this loop with less the two steps coz we have to jump next step so that we will not get out of bound
        //i start with n-1 and -2 for two jump we already added to dp[]
        for(int i = (n-1)-2 ; i >= 0; i--)
        //why we started this loop form max to low?
        //coz we wanted to go to destination with storing minimun cost required to reach for input to next element/stair##tabulation
        {
            //one stair can jump a 1 or 2 jump(xi, yi)
            //so take those jump and ask which cost us less
            int min = Math.min(dp[i+1], dp[i+2]);
            //which ever cost the less add those in first step which is X + xi
            dp[i] = cost[i] + min;
        }
        
        //in mention in problem statement we can start with oth step or 1st step
        if(dp[0] < dp[1])
        return dp[0];
        else return dp[1];
    }
0
saurabhsharma295202 months ago
O(N) Solution:
 
int a = 0, b = 0, c= 0;
       for(int i = 2;i<=N;i++){
           c = min(a+cost[i-2], b+cost[i-1]);
           a = b;
           b = c;
           
       }
       return c;
0
shreyashyewale849642 months ago
class Solution {   static int minCostClimbingStairs(int[] cost , int n) {       //Write your code here       int dp[]=new int[n];       dp[n-1]=cost[n-1];       dp[n-2]=cost[n-2];       for(int i=n-3;i>=0;i--)       {           dp[i]=cost[i]+Math.min(dp[i+1],dp[i+2]);       }       return dp[0]<dp[1]?dp[0]:dp[1];   }};
0
madhukartemba
This comment was deleted.
+1
abhixhek052 months ago
int minCostClimbingStairs(vector<int>&cost ,int n) {
        
        for(int i=2;i<cost.size();i++){
            cost[i] += min(cost[i-1],cost[i-2]);
        }
        int k = min(cost[n-1],cost[n-2]);
        return k;
    }
0
gulshan993 months ago
class Solution { public:     int dp[1001];     int helper(int i,vector<int>&cost)     {         if(i>=cost.size())return 0;         if(dp[i]!=-1)return dp[i];         int op1=helper(i+1,cost);         int op2=helper(i+2,cost);         return dp[i]=min(op1,op2)+cost[i];     }   int minCostClimbingStairs(vector<int>&cost ,int N) {       //Write your code here       memset(dp,-1,sizeof dp);       return min(helper(0,cost),helper(1,cost));   }};
0
satpalbhardwaj6653 months ago
int minCostClimbingStairs(vector<int>&cost ,int N) {
       //Write your code here
       int a = 0, b = 0, temp = 0;
       for(int j=N-1; j>=0; j--)
       {
           temp = cost[j]+min(a, b);
           b = a;
           a = temp;
       }
       return min(a, b);
   }
Time : 0.0
Space : O(1)
Language: CPP
0
shrutieddula4 months ago
def minCostClimbingStairs(self, cost, N):       cost.append(0)       for i in range(len(cost)-3,-1,-1):           cost[i]=min(cost[i]+cost[i+1],cost[i]+cost[i+2])       return min(cost[0],cost[1])   
 
0
Xi Jinping8 months ago
Xi Jinping
int minCostClimbingStairs(vector<int>&cost ,int N) {
        int dp0=cost[0];        int dp1=cost[1];        if(N<=2)        return min(dp1,dp0);        int dp3;        int dp4=dp1;
        for(int i=2;i<n;i++) {="" dp3="cost[i]+min(dp0,dp1);" if(i="=N-2)" dp4="dp3;" dp0="dp1;" dp1="dp3;" }="" return="" min(dp4,dp3)="" ;="" }="">
0
ANSH8 months ago
ANSH
 int minCostClimbingStairs(vector<int>&cost ,int N) {         int dp0=cost[0];        int dp1=cost[1];        if(N<=2)        return min(dp1,dp0);        int dp3;        int dp4=dp1;          for(int i=2;i<n;i++) {="" dp3="cost[i]+min(dp0,dp1);" if(i="=N-2)" dp4="dp3;" dp0="dp1;" dp1="dp3;" }="" return="" min(dp4,dp3)="" ;="" }="" <="" code="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.