给定一个整数X和一个由N 个整数组成的数组cost[] ,任务是根据以下约束找到从第一个元素开始到达给定数组末尾的最小成本:
- 访问点i的成本是cost[i] ,其中1 ≤ i ≤ N 。
- 跳过点i 的成本是min(cost[i], X)其中1 ≤ i ≤ N 。
- 最多可以连续跳过2个点。
- 不能跳过第一个和最后一个位置。
例子:
Input: N = 6, X = 4, cost[] = {6, 3, 9, 2, 1, 3}
Output: 19
Explanation:
Follow the steps below:
Step 1: Choose element at 1. Sum = 6
Step 2: Choose element at 2. Sum = 6 + 3 = 9
Step 3: Skip element at 3. Sum = 6 + 3 + 4 = 13
Step 4: Choose element at 4. Sum = 6 + 3 + 4 + 2 = 15
Step 5: Choose element at 5. Sum = 6 + 3 + 4 + 2 + 1 = 16
Step 6: Choose element at 6. Sum = 6 + 3 + 4 + 2 + 1 + 3 = 19
Hence, the minimum cost is 19.
Input: N = 7, X = 4, cost[] = {6, 3, 9, 2, 1, 3, 4}
Output: 23
Explanation:
Follow the steps below:
Step 1: Choose element at 1. Sum = 6
Step 2: Choose element at 2. Sum = 6+3 = 9
Step 3: Skip element at 3. Sum = 6+3+4 = 13
Step 4: Choose element at 4. Sum = 6 + 3 + 4 + 2 = 15
Step 5: Choose element at 5. Sum = 6+3+4+2+1 = 16
Step 6: Choose element at 6. Sum = 6+3+4+2+1+3 = 19
Step 7: Choose element at 6. Sum = 6 + 3 + 4 + 2 + 1 + 3 + 4 = 23
Hence, the minimum cost is 23.
朴素的方法:最简单的方法是通过考虑或跳过某些位置来生成所有可能的解决方案。每个元素有两个选项,即可以跳过或可以选择。因此,最多可以有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[i] = cost[i] + min(dp[i-1], min(2*X + dp[i-2], 2*X + dp[i-3]))
where,
dp[i] stores the minimum answer to reach position i from position 0.
请按照以下步骤解决问题:
- 初始化一个数组dp[] ,其中dp[i]将存储从位置0到达位置i的最小答案。
- 在[0, N – 1]范围内遍历给定数组cost[]并在每个位置i更新dp[i]为:
cost[i] + min(dp[i-1], min(2*X + dp[i-2], 2*X + dp[i-3]))
- 完成上述步骤后,打印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
Javascript
19
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live