Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum cost to reach the top of the floor by climbing stairs

  • Difficulty Level : Easy
  • Last Updated : 14 May, 2021

Given N non-negative integers which signifies the cost of the moving from each stair. Paying the cost at i-th step, you can either climb one or two steps. Given that one can start from the 0-the step or 1-the step, the task is to find the minimum cost to reach the top of the floor(N+1) by climbing N stairs. 

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: a[] = { 16, 19, 10, 12, 18 }
Output: 31
Start from 19 and then move to 12. 

Input: a[] = {2, 5, 3, 1, 7, 3, 4}
Output: 9 
2->3->1->3

Approach: Let dp[i] be the cost to climb the i-th staircase to from 0-th or 1-th step. Hence dp[i] = cost[i] + min(dp[i-1], dp[i-2]). Since dp[i-1] and dp[i-2] are needed to compute the cost of traveling from i-th step, a bottom-up approach can be used to solve the problem. The answer will be the minimum cost of reaching n-1th stair and n-2th stair. Compute the dp[] array in a bottom-up manner. 



Below is the implementation of the above approach.  

C++




// C++ program to find the minimum
// cost required to reach the n-th floor
#include <bits/stdc++.h>
using namespace std;
 
// function to find the minimum cost
// to reach N-th floor
int minimumCost(int cost[], int n)
{
    // declare an array
    int dp[n];
 
    // base case
    if (n == 1)
        return cost[0];
 
    // initially to climb till 0-th
    // or 1th stair
    dp[0] = cost[0];
    dp[1] = cost[1];
 
    // iterate for finding the cost
    for (int i = 2; i < n; i++) {
        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
    }
 
    // return the minimum
    return min(dp[n - 2], dp[n - 1]);
}
 
// Driver Code
int main()
{
    int a[] = { 16, 19, 10, 12, 18 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << minimumCost(a, n);
    return 0;
}

Java




// Java program to find the
// minimum cost required to
// reach the n-th floor
import java.io.*;
import java.util.*;
 
class GFG
{
// function to find
// the minimum cost
// to reach N-th floor
static int minimumCost(int cost[],
                       int n)
{
    // declare an array
    int dp[] = new int[n];
 
    // base case
    if (n == 1)
        return cost[0];
 
    // initially to
    // climb till 0-th
    // or 1th stair
    dp[0] = cost[0];
    dp[1] = cost[1];
 
    // iterate for finding the cost
    for (int i = 2; i < n; i++)
    {
        dp[i] = Math.min(dp[i - 1],
                         dp[i - 2]) + cost[i];
    }
 
    // return the minimum
    return Math.min(dp[n - 2],
                    dp[n - 1]);
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 16, 19, 10, 12, 18 };
    int n = a.length;
    System.out.print(minimumCost(a, n));
}
}

Python3




# Python3 program to find
# the minimum cost required
# to reach the n-th floor
 
# function to find the minimum
# cost to reach N-th floor
def minimumCost(cost, n):
 
    # declare an array
    dp = [None]*n
 
    # base case
    if n == 1:
        return cost[0]
 
    # initially to climb
    # till 0-th or 1th stair
    dp[0] = cost[0]
    dp[1] = cost[1]
 
    # iterate for finding the cost
    for i in range(2, n):
        dp[i] = min(dp[i - 1],
                    dp[i - 2]) + cost[i]
 
    # return the minimum
    return min(dp[n - 2], dp[n - 1])
 
# Driver Code
if __name__ == "__main__":
    a = [16, 19, 10, 12, 18 ]
    n = len(a)
    print(minimumCost(a, n))
 
# This code is contributed
# by ChitraNayal

C#




// C# program to find the
// minimum cost required to
// reach the n-th floor
using System;
 
class GFG
{
// function to find
// the minimum cost
// to reach N-th floor
static int minimumCost(int[] cost,
                       int n)
{
    // declare an array
    int []dp = new int[n];
 
    // base case
    if (n == 1)
        return cost[0];
 
    // initially to
    // climb till 0-th
    // or 1th stair
    dp[0] = cost[0];
    dp[1] = cost[1];
 
    // iterate for finding the cost
    for (int i = 2; i < n; i++)
    {
        dp[i] = Math.Min(dp[i - 1],
                         dp[i - 2]) + cost[i];
    }
 
    // return the minimum
    return Math.Min(dp[n - 2],
                    dp[n - 1]);
}
 
// Driver Code
public static void Main()
{
    int []a = { 16, 19, 10, 12, 18 };
    int n = a.Length;
    Console.WriteLine(minimumCost(a, n));
}
}
 
// This code is contributed
// by Subhadeep

PHP




<?php
// PHP program to find the
// minimum cost required
// to reach the n-th floor
 
// function to find the minimum
// cost to reach N-th floor
function minimumCost(&$cost, $n)
{
    // declare an array
 
    // base case
    if ($n == 1)
        return $cost[0];
 
    // initially to climb
    // till 0-th or 1th stair
    $dp[0] = $cost[0];
    $dp[1] = $cost[1];
 
    // iterate for finding
    // the cost
    for ($i = 2; $i < $n; $i++)
    {
        $dp[$i] = min($dp[$i - 1],
                      $dp[$i - 2]) +
                      $cost[$i];
    }
 
    // return the minimum
    return min($dp[$n - 2],
               $dp[$n - 1]);
}
 
// Driver Code
$a = array(16, 19, 10, 12, 18);
$n = sizeof($a);
echo(minimumCost($a, $n));
     
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




<script>
 
// Javascript program to find the
// minimum cost required to
// reach the n-th floor
 
     
    // function to find
// the minimum cost
// to reach N-th floor   
    function minimumCost(cost,n)
    {
        // declare an array
    let dp = new Array(n);
  
    // base case
    if (n == 1)
        return cost[0];
  
    // initially to
    // climb till 0-th
    // or 1th stair
    dp[0] = cost[0];
    dp[1] = cost[1];
  
    // iterate for finding the cost
    for (let i = 2; i < n; i++)
    {
        dp[i] = Math.min(dp[i - 1],
                         dp[i - 2]) + cost[i];
    }
  
    // return the minimum
    return Math.min(dp[n - 2],
                    dp[n - 1]);
    }
     
    // Driver Code
    let a=[16, 19, 10, 12, 18 ];
    let n = a.length;
    document.write(minimumCost(a, n));
     
 
// This code is contributed by rag2127
 
</script>
Output: 
31

 

Time Complexity: O(N) 
Auxiliary Space: O(N)

Space-optimized Approach: Instead of using dp[] array for memoizing the cost, use two-variable dp1 and dp2. Since the cost of reaching the last two stairs is required only, use two variables and update them by swapping when one stair is climbed. 

Below is the implementation of the above approach:  

C++




// C++ program to find the minimum
// cost required to reach the n-th floor
// space-optimized solution
#include <bits/stdc++.h>
using namespace std;
 
// function to find the minimum cost
// to reach N-th floor
int minimumCost(int cost[], int n)
{
    int dp1 = 0, dp2 = 0;
 
    // traverse till N-th stair
    for (int i = 0; i < n; i++) {
        int dp0 = cost[i] + min(dp1, dp2);
 
        // update the last two stairs value
        dp2 = dp1;
        dp1 = dp0;
    }
    return min(dp1, dp2);
}
// Driver Code
int main()
{
    int a[] = { 2, 5, 3, 1, 7, 3, 4 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << minimumCost(a, n);
    return 0;
}

Java




// Java program to find the
// minimum cost required to
// reach the n-th floor
// space-optimized solution
import java.io.*;
import java.util.*;
 
class GFG
{
// function to find
// the minimum cost
// to reach N-th floor
static int minimumCost(int cost[], int n)
{
    int dp1 = 0, dp2 = 0;
 
    // traverse till N-th stair
    for (int i = 0; i < n; i++)
    {
        int dp0 = cost[i] +
                  Math.min(dp1, dp2);
 
        // update the last
        // two stairs value
        dp2 = dp1;
        dp1 = dp0;
    }
    return Math.min(dp1, dp2);
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 2, 5, 3, 1, 7, 3, 4 };
    int n = a.length;
    System.out.print(minimumCost(a, n));
}
}

Python3




# Python3 program to find
# the minimum cost required
# to reach the n-th floor
# space-optimized solution
 
# function to find the minimum
# cost to reach N-th floor
def minimumCost(cost, n):
 
    dp1 = 0
    dp2 = 0
 
    # traverse till N-th stair
    for i in range(n):
        dp0 = cost[i] + min(dp1, dp2)
 
        # update the last
        # two stairs value
        dp2 = dp1
        dp1 = dp0
    return min(dp1, dp2)
 
# Driver Code
if __name__ == "__main__":
    a = [ 2, 5, 3, 1, 7, 3, 4 ]
    n = len(a)
    print(minimumCost(a, n))
     
# This code is contributed
# by ChitraNayal

C#




// C# program to find the
// minimum cost required to
// reach the n-th floor
// space-optimized solution
using System;
 
class GFG
{
// function to find
// the minimum cost
// to reach N-th floor
static int minimumCost(int[] cost,
                       int n)
{
    int dp1 = 0, dp2 = 0;
 
    // traverse till N-th stair
    for (int i = 0; i < n; i++)
    {
        int dp0 = cost[i] +
                  Math.Min(dp1, dp2);
 
        // update the last
        // two stairs value
        dp2 = dp1;
        dp1 = dp0;
    }
    return Math.Min(dp1, dp2);
}
 
// Driver Code
public static void Main()
{
    int[] a = { 2, 5, 3, 1, 7, 3, 4 };
    int n = a.Length;
    Console.Write(minimumCost(a, n));
}
}
 
// This code is contributed
// by ChitraNayal

PHP




<?php
// PHP program to find the
// minimum cost required to
// reach the n-th floor
// space-optimized solution
 
// function to find the minimum
// cost to reach N-th floor
function minimumCost(&$cost, $n)
{
    $dp1 = 0;
    $dp2 = 0;
 
    // traverse till N-th stair
    for ($i = 0; $i < $n; $i++)
    {
        $dp0 = $cost[$i] +
               min($dp1, $dp2);
 
        // update the last
        // two stairs value
        $dp2 = $dp1;
        $dp1 = $dp0;
    }
    return min($dp1, $dp2);
}
// Driver Code
$a = array(2, 5, 3, 1, 7, 3, 4);
$n = sizeof($a);
echo (minimumCost($a, $n));
 
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




<script>
// Javascript program to find the
// minimum cost required to
// reach the n-th floor
// space-optimized solution
     
    // function to find
// the minimum cost
// to reach N-th floor
    function minimumCost(cost,n)
    {
        let dp1 = 0, dp2 = 0;
  
    // traverse till N-th stair
    for (let i = 0; i < n; i++)
    {
        let dp0 = cost[i] +
                  Math.min(dp1, dp2);
  
        // update the last
        // two stairs value
        dp2 = dp1;
        dp1 = dp0;
    }
    return Math.min(dp1, dp2);
    }
     
    // Driver Code
    let a=[2, 5, 3, 1, 7, 3, 4 ];
    let n = a.length;
    document.write(minimumCost(a, n));
     
 
// This code is contributed by avanitrachhadiya2155
</script>
Output: 
9

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

The following problem can be solved using top-down approach. In that case the recurrence will be dp[i] = cost[i] + min(dp[i+1], dp[i+2]). 
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!