给定一个由N 个正整数组成的数组arr[] ,任务是通过仅移动到索引(i + 2)和(i – 1)来找到穿过数组或到达数组末尾所需的最小成本第i个索引。
例子:
Input: arr[] = {5, 1, 2, 10, 100}
Output: 18
Explanation:
Optimal cost path (0 based indexing): 0 → 2 → 1 → 3 → 5
Therefore, the minimum cost = 5 + 2 + 1 + 10 = 18.
Input: arr[] = {9, 4, 6, 8, 5}
Output: 20
Explanation:
Optimal cost path (0 based indexing): 0 → 2 → 4
Therefore, the minimum cost = 9 + 6 + 5 = 20
朴素的方法:可以根据以下观察解决给定的问题:
- 由于所有成本都是正的,因此向后移动多于一步永远不是最佳选择,因此要到达数组的特定索引i ,要么直接从第(i – 2)个索引跳转,要么从(i – 1) th到(i + 1) th index,即(向前跳2次),后跟向后跳1次,即从(i + 1) th index到第i index。
- 现在,从数组的末尾递归遍历索引(i – 2)和(i – 1)处的元素,计算两者的最小成本。因此,可以使用以下递推关系计算交叉阵列的最小成本:
minCost(index) = minimum(minCost(index – 2) + arr[i], minCost(index – 1) + arr[i] + arr[i + 1])
时间复杂度: O(2 N )
辅助空间: O(1)
高效的方法:上面讨论的方法有最优子结构和重叠子问题。因此,它可以通过使用 Memoization 或 Tabulation 进行优化。请按照以下步骤解决问题:
- 初始化一个数组dp[] ,其中dp[i]存储到达第i个索引的最小成本。
- 将dp[0] = arr[0]初始化为到达第0个索引的成本,它等于第0个索引本身的值。更新DP [1] = ARR [0] + ARR [1] + ARR [2],作为达到第1个索引,跳从第0索引第二索引indexn给第一索引。
- 使用变量i迭代范围[2, N – 2]并将dp[i]更新为(dp[i – 2] + arr[i])和(dp[i – 1] + arr[i] ] + arr[i + 1]) 。
- 对于最后一个索引(N – 1) ,将dp[N – 1]更新为(dp[N – 3] + arr[N – 1])和(dp[N – 2]) 的最小值。
- 完成以上步骤后,打印dp[N-1]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
// to reach the end of an array
void minCost(int arr[], int n)
{
// Base Case: When N < 3
if (n < 3) {
cout << arr[0];
return;
}
// Store the results in table
int* dp = new int[n];
// Initialize base cases
dp[0] = arr[0];
dp[1] = dp[0] + arr[1] + arr[2];
// Iterate over the range[2, N - 2]
// to construct the dp array
for (int i = 2; i < n - 1; i++)
dp[i] = min(dp[i - 2] + arr[i],
dp[i - 1] + arr[i]
+ arr[i + 1]);
// Handle case for the last index, i.e. N - 1
dp[n - 1] = min(dp[n - 2],
dp[n - 3] + arr[n - 1]);
// Print the answer
cout << dp[n - 1];
}
// Driver Code
int main()
{
int arr[] = { 9, 4, 6, 8, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
minCost(arr, N);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the minimum cost
// to reach the end of an array
static void minCost(int arr[], int n)
{
// Base Case: When N < 3
if (n < 3) {
System.out.println(arr[0]);
return;
}
// Store the results in table
int dp[] = new int[n];
// Initialize base cases
dp[0] = arr[0];
dp[1] = dp[0] + arr[1] + arr[2];
// Iterate over the range[2, N - 2]
// to construct the dp array
for (int i = 2; i < n - 1; i++)
dp[i] = Math.min(dp[i - 2] + arr[i],
dp[i - 1] + arr[i] + arr[i + 1]);
// Handle case for the last index, i.e. N - 1
dp[n - 1] = Math.min(dp[n - 2], dp[n - 3] + arr[n - 1]);
// Print the answer
System.out.println(dp[n - 1]);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 9, 4, 6, 8, 5 };
int N = arr.length;
minCost(arr, N);
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to find the minimum cost
# to reach the end of an array
def minCost(arr, n):
# Base Case: When N < 3
if (n < 3):
print(arr[0])
return
# Store the results in table
dp = [0] * n
# Initialize base cases
dp[0] = arr[0]
dp[1] = dp[0] + arr[1] + arr[2]
# Iterate over the range[2, N - 2]
# to construct the dp array
for i in range(2, n - 1):
dp[i] = min(dp[i - 2] + arr[i],
dp[i - 1] + arr[i]
+ arr[i + 1])
# Handle case for the last index, i.e. N - 1
dp[n - 1] = min(dp[n - 2],
dp[n - 3] + arr[n - 1])
# Print the answer
print(dp[n - 1])
# Driver Code
if __name__ == "__main__":
arr = [9, 4, 6, 8, 5]
N = len(arr)
minCost(arr, N)
# This code is contributed by ukasp.
C#
// C# Program to implement
// the above approach
using System;
public class GFG
{
// Function to find the minimum cost
// to reach the end of an array
static void minCost(int []arr, int n)
{
// Base Case: When N < 3
if (n < 3) {
Console.WriteLine(arr[0]);
return;
}
// Store the results in table
int []dp = new int[n];
// Initialize base cases
dp[0] = arr[0];
dp[1] = dp[0] + arr[1] + arr[2];
// Iterate over the range[2, N - 2]
// to construct the dp array
for (int i = 2; i < n - 1; i++)
dp[i] = Math.Min(dp[i - 2] + arr[i],
dp[i - 1] + arr[i] + arr[i + 1]);
// Handle case for the last index, i.e. N - 1
dp[n - 1] = Math.Min(dp[n - 2], dp[n - 3] + arr[n - 1]);
// Print the answer
Console.WriteLine(dp[n - 1]);
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 9, 4, 6, 8, 5 };
int N = arr.Length;
minCost(arr, N);
}
}
// This code is contributed by AnkThon
Javascript
20
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。