📜  爬楼梯到达顶层的最低成本

📅  最后修改于: 2021-09-22 10:04:56             🧑  作者: 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] 是从第 i 个楼梯爬到第 0 步或第 1 步的成本。因此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


Javascript


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


Javascript


输出:

31

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

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

下面是上述方法的实现:

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));
}
}

蟒蛇3

# 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


Javascript


输出:
9

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

下面的问题可以使用自顶向下的方法来解决。在这种情况下,循环将是dp[i] = cost[i] + min(dp[i+1], dp[i+2])。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程