📌  相关文章
📜  每次移动两次向前跳或一次向后跳,将到达阵列末尾的成本降至最低

📅  最后修改于: 2021-04-17 16:56:20             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是通过仅从以下位置移到索引(i + 2)(i – 1)来找到穿越数组或到达数组末尾所需的最低成本。第i索引。

例子:

幼稚的方法:可以根据以下观察结果解决给定的问题:

  • 由于所有成本都是正数,因此向后移动不止一个步,从而到达数组的特定索引i (直接从第(i – 2)索引跳转或从(i – 1)(i + 1)索引,即(2向前跳跃),然后向后1个跳跃,即第(i + 1)索引至i索引。
  • 现在,从数组的末尾开始递归遍历,对于索引(i – 2)(i – 1)的元素,计算两者的最小开销。因此,可以使用以下递归关系来计算穿越数组的最小成本:

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

高效的方法:上面讨论的方法同时具有最佳子结构和重叠子问题。因此,可以通过使用“记忆化”或“制表”对其进行优化。请按照以下步骤解决问题:

  • 初始化数组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


输出:
20

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