📌  相关文章
📜  最小化选择和跳过数组元素以到达给定数组末尾的成本

📅  最后修改于: 2021-05-17 19:04:55             🧑  作者: Mango

给定一个整数X和一个由N个整数组成的数组cost [] ,任务是根据以下约束,找到从第一个元素开始到达给定数组末尾的最小成本:

  • 访问点i成本cost [i] ,其中1≤i≤N
  • 跳过点i的成本min(cost [i],X) ,其中1≤i≤N
  • 最多可以连续跳过2点。
  • 不能跳过第一个和最后一个位置。

例子:

天真的方法:最简单的方法是通过考虑或跳过某些位置来生成所有可能的解决方案。每个元素有两个选项,即可以跳过或选择。因此,最多可以有2 N个组合。检查每种组合中跳过的位置不超过3个。在这些组合中,选择成本最低的一种并打印最低成本。

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

高效方法:为优化上述方法,其思想是使用动态编程,并观察到,如果跳过任何位置i ,则成本由cost [i]X增加,但如果成本由cost [i]增加,则最好选择该职位,因为选择该职位也会使cost [i]增加成本。这意味着可以通过以下方法找到到达位置i的最低成本:在到达位置的最低成本(i – 1)中取最小值,再将X +到达位置的最低成本(i – 2)中取最小值,将2X +到达位置的最低成本到达位置(i – 3)

因此,dp转换如下:

请按照以下步骤解决问题:

  • 初始化数组dp [] ,其中dp [i]将存储从位置0到达位置i的最小答案。
  • [0,N – 1]范围内遍历给定的数组cost []并在每个位置i处将dp [i]更新为:
  • 完成上述步骤后,打印dp [N – 1] ,该答案存储从位置0到位置(N – 1)的答案。  

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum cost
// to reach the end of the array from
// the first element
void minimumCost(int* cost, int n, int x)
{
    // Store the results
    vector dp(n + 2, 0);
 
    // Consider first index cost
    dp[0] = cost[0];
 
    // Find answer for each position i
    for (int i = 1; i < n; i++) {
 
        // First Element
        if (i == 1)
            dp[i] = cost[i] + dp[i - 1];
 
        // Second Element
        if (i == 2)
            dp[i] = cost[i]
                    + min(dp[i - 1],
                          x + dp[i - 2]);
 
        // For remaining element
        if (i >= 3)
 
            // Consider min cost for
            // skipping
            dp[i] = cost[i]
                    + min(dp[i - 1],
                          min(x + dp[i - 2],
                              2 * x + dp[i - 3]));
    }
 
    // Last index represents the
    // minimum total cost
    cout << dp[n - 1];
}
 
// Driver Code
int main()
{
    // Given X
    int X = 4;
 
    // Given array cost[]
    int cost[] = { 6, 3, 9, 2, 1, 3 };
 
    int N = sizeof(cost) / sizeof(cost[0]);
 
    // Function Call
    minimumCost(cost, N, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum cost
// to reach the end of the array from
// the first element
static void minimumCost(int[] cost, int n, int x)
{
     
    // Store the results
    int[] dp = new int[n + 2];
 
    // Consider first index cost
    dp[0] = cost[0];
 
    // Find answer for each position i
    for(int i = 1; i < n; i++)
    {
         
        // First Element
        if (i == 1)
            dp[i] = cost[i] + dp[i - 1];
 
        // Second Element
        if (i == 2)
            dp[i] = cost[i] + Math.min(dp[i - 1],
                                   x + dp[i - 2]);
 
        // For remaining element
        if (i >= 3)
 
            // Consider min cost for
            // skipping
            dp[i] = cost[i] + Math.min(dp[i - 1],
                          Math.min(x + dp[i - 2],
                               2 * x + dp[i - 3]));
    }
 
    // Last index represents the
    // minimum total cost
    System.out.println(dp[n - 1]);
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given X
    int X = 4;
 
    // Given array cost[]
    int[] cost = { 6, 3, 9, 2, 1, 3 };
 
    int N = cost.length;
 
    // Function Call
    minimumCost(cost, N, X);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to find the minimum cost
# to reach the end of the array from
# the first element
def minimumCost(cost, n, x):
     
    # Store the results
    dp = [0] * (n + 2)
 
    # Consider first index cost
    dp[0] = cost[0]
 
    # Find answer for each position i
    for i in range(1, n):
         
        # First Element
        if (i == 1):
            dp[i] = cost[i] + dp[i - 1]
 
        # Second Element
        if (i == 2):
            dp[i] = cost[i] + min(dp[i - 1],
                              x + dp[i - 2])
 
        # For remaining element
        if (i >= 3):
 
            # Consider min cost for
            # skipping
            dp[i] = (cost[i] +
                   min(dp[i - 1],
                   min(x + dp[i - 2],
                   2 * x + dp[i - 3])))
 
    # Last index represents the
    # minimum total cost
    print(dp[n - 1])
 
# Driver Code
if __name__ == '__main__':
     
    # Given X
    X = 4
 
    # Given array cost[]
    cost = [ 6, 3, 9, 2, 1, 3 ]
 
    N = len(cost)
 
    # Function Call
    minimumCost(cost, N, X)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum cost
// to reach the end of the array from
// the first element
static void minimumCost(int[] cost, int n, int x)
{
     
    // Store the results
    int[] dp = new int[n + 2];
 
    // Consider first index cost
    dp[0] = cost[0];
 
    // Find answer for each position i
    for(int i = 1; i < n; i++)
    {
         
        // First Element
        if (i == 1)
            dp[i] = cost[i] + dp[i - 1];
 
        // Second Element
        if (i == 2)
            dp[i] = cost[i] + Math.Min(dp[i - 1],
                                   x + dp[i - 2]);
 
        // For remaining element
        if (i >= 3)
         
            // Consider min cost for
            // skipping
            dp[i] = cost[i] + Math.Min(dp[i - 1],
                          Math.Min(x + dp[i - 2],
                               2 * x + dp[i - 3]));
    }
 
    // Last index represents the
    // minimum total cost
    Console.WriteLine(dp[n - 1]);
}
 
// Driver Code
public static void Main()
{
     
    // Given X
    int X = 4;
 
    // Given array cost[]
    int[] cost = { 6, 3, 9, 2, 1, 3 };
 
    int N = cost.Length;
 
    // Function Call
    minimumCost(cost, N, X);
}
}
 
// This code is contributed by akhilsaini


输出:
19





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