📜  爬楼梯到达地板顶部的最低成本

📅  最后修改于: 2021-04-27 20:26:06             🧑  作者: Mango

给定N个非负整数,表示从每个楼梯移出的费用。在第i步支付费用时,您可以爬一或两步。假设可以从0步或1步开始,那么任务是通过爬N个楼梯找到到达地板顶部(N + 1)的最低成本。

例子:

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

方法:令dp [i]为从第0步或第1步爬上第i步的成本。因此, dp [i] = cost [i] + min(dp [i-1],dp [i-2]) 。由于需要dp [i-1]和dp [i-2]来计算从第i步起的行驶成本,因此可以使用自下而上的方法来解决该问题。答案将是到达第n-1楼梯和第n-2楼梯的最低成本。以自下而上的方式计算dp []数组。

下面是上述方法的实现。

C++
// C++ program to find the minimum
// cost required to reach the n-th floor
#include 
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


C++
// C++ program to find the minimum
// cost required to reach the n-th floor
// space-optimized solution
#include 
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


输出:
31

时间复杂度: O(N)
辅助空间: O(N)

空间优化方法:使用两个变量dp1和dp2,而不是使用dp []数组来记录成本。由于仅需要到达最后两个楼梯的费用,因此请使用两个变量,并在爬上一个楼梯时通过交换来更新它们。

下面是上述方法的实现:

C++

// C++ program to find the minimum
// cost required to reach the n-th floor
// space-optimized solution
#include 
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


输出:
9

时间复杂度: O(N)
辅助空间: O(1)

使用自上而下的方法可以解决以下问题。在那种情况下,重复率将为dp [i] = cost [i] + min(dp [i + 1],dp [i + 2])。