📌  相关文章
📜  最小化成本以使数组中的所有相邻元素都不同

📅  最后修改于: 2021-09-07 04:34:32             🧑  作者: Mango

给定两个大小为N 的整数数组arr[]cost[] ,任务是以最小成本使所有相邻元素不同。 cost[i]表示将i元素增加 1 的成本。
例子:

方法:

  • 我们可以观察到,一个元素可能需要最多增加两次。
  • 这个问题可以用动态规划解决。
  • 创建一个 DP 表dp[][] ,其中行代表元素,列代表增量。
  • dp[i][j]是使用j增量使i元素与其相邻元素不同所需的最小成本。
  • dp[i][j] 的值可以计算为:

下面是上述方法的实现

C++
// C++ program to find the
// minimum cost required to make
// all adjacent elements disinct
 
#include 
using namespace std;
 
// Function that prints minimum cost required
void minimumCost(int arr[], int cost[], int N)
{
 
    // Dp-table
    vector > dp(N, vector(3));
 
    // Base case
    // Not increasing the first element
    dp[0][0] = 0;
 
    // Increasing the first element by 1
    dp[0][1] = cost[0];
 
    // Increasing the first element by 2
    dp[0][2] = cost[0] * 2;
 
    for (int i = 1; i < N; i++) {
        for (int j = 0; j < 3; j++) {
 
            int minimum = 1e6;
 
            // Condition if current element
            // is not equal to previous
            // non-increased element
            if (j + arr[i] != arr[i - 1])
                minimum
                    = min(minimum,
                          dp[i - 1][0]);
 
            // Condition if current element
            // is not equal to previous element
            // after being increased by 1
            if (j + arr[i] != arr[i - 1] + 1)
                minimum
                    = min(minimum,
                          dp[i - 1][1]);
 
            // Condition if current element
            // is not equal to previous element
            // after being increased by 2
            if (j + arr[i] != arr[i - 1] + 2)
                minimum
                    = min(minimum,
                          dp[i - 1][2]);
 
            // Take the minimum from all cases
            dp[i][j] = j * cost[i] + minimum;
        }
    }
 
    int ans = 1e6;
 
    // Finding the minimum cost
    for (int i = 0; i < 3; i++)
        ans = min(ans, dp[N - 1][i]);
 
    // Printing the minimum cost
    // required to make all adjacent
    // elements disinct
    cout << ans << "\n";
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 2, 2, 3, 4 };
    int cost[] = { 3, 2, 5, 4, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumCost(arr, cost, N);
 
    return 0;
}


Java
// Java program to find the minimum
// cost required to make all
// adjacent elements disinct
import java.util.*;
 
class GFG{
 
// Function that prints minimum cost required
static void minimumCost(int arr[], int cost[],
                        int N)
{
 
    // Dp-table
    int [][]dp = new int[N][3];
 
    // Base case
    // Not increasing the first element
    dp[0][0] = 0;
 
    // Increasing the first element by 1
    dp[0][1] = cost[0];
 
    // Increasing the first element by 2
    dp[0][2] = cost[0] * 2;
 
    for(int i = 1; i < N; i++)
    {
       for(int j = 0; j < 3; j++)
       {
          int minimum = (int) 1e6;
           
          // Condition if current element
          // is not equal to previous
          // non-increased element
          if (j + arr[i] != arr[i - 1])
              minimum = Math.min(minimum, dp[i - 1][0]);
           
          // Condition if current element
          // is not equal to previous element
          // after being increased by 1
          if (j + arr[i] != arr[i - 1] + 1)
              minimum = Math.min(minimum, dp[i - 1][1]);
           
          // Condition if current element
          // is not equal to previous element
          // after being increased by 2
          if (j + arr[i] != arr[i - 1] + 2)
              minimum = Math.min(minimum, dp[i - 1][2]);
 
          // Take the minimum from all cases
          dp[i][j] = j * cost[i] + minimum;
       }
    }
    int ans = (int) 1e6;
 
    // Finding the minimum cost
    for(int i = 0; i < 3; i++)
       ans = Math.min(ans, dp[N - 1][i]);
 
    // Printing the minimum cost
    // required to make all adjacent
    // elements disinct
    System.out.print(ans + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 2, 2, 3, 4 };
    int cost[] = { 3, 2, 5, 4, 2, 1 };
    int N = arr.length;
 
    minimumCost(arr, cost, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the
# minimum cost required to make
# all adjacent elements disinct
 
# Function that prints minimum cost required
def minimumCost(arr, cost, N):
 
    # Dp-table
    dp = [[0 for i in range(3)] for i in range(N)]
 
    # Base case
    # Not increasing the first element
    dp[0][0] = 0
 
    # Increasing the first element by 1
    dp[0][1] = cost[0]
 
    # Increasing the first element by 2
    dp[0][2] = cost[0] * 2
 
    for i in range(1, N):
        for j in range(3):
 
            minimum = 1e6
 
            # Condition if current element
            # is not equal to previous
            # non-increased element
            if (j + arr[i] != arr[i - 1]):
                minimum = min(minimum, dp[i - 1][0])
 
            # Condition if current element
            # is not equal to previous element
            # after being increased by 1
            if (j + arr[i] != arr[i - 1] + 1):
                minimum = min(minimum, dp[i - 1][1])
 
            # Condition if current element
            # is not equal to previous element
            # after being increased by 2
            if (j + arr[i] != arr[i - 1] + 2):
                minimum = min(minimum, dp[i - 1][2])
 
            # Take the minimum from all cases
            dp[i][j] = j * cost[i] + minimum
             
    ans = 1e6
 
    # Finding the minimum cost
    for i in range(3):
        ans = min(ans, dp[N - 1][i])
 
    # Printing the minimum cost
    # required to make all adjacent
    # elements disinct
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 1, 2, 2, 3, 4 ]
    cost = [ 3, 2, 5, 4, 2, 1 ]
    N = len(arr)
 
    minimumCost(arr, cost, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the minimum
// cost required to make all
// adjacent elements disinct
using System;
 
class GFG{
 
// Function that prints minimum cost required
static void minimumCost(int []arr, int []cost,
                        int N)
{
 
    // Dp-table
    int [,]dp = new int[N, 3];
 
    // Base case
    // Not increasing the first element
    dp[0, 0] = 0;
 
    // Increasing the first element by 1
    dp[0, 1] = cost[0];
 
    // Increasing the first element by 2
    dp[0, 2] = cost[0] * 2;
 
    for(int i = 1; i < N; i++)
    {
       for(int j = 0; j < 3; j++)
       {
          int minimum = (int) 1e6;
           
          // Condition if current element
          // is not equal to previous
          // non-increased element
          if (j + arr[i] != arr[i - 1])
              minimum = Math.Min(minimum,
                                 dp[i - 1, 0]);
           
          // Condition if current element
          // is not equal to previous element
          // after being increased by 1
          if (j + arr[i] != arr[i - 1] + 1)
              minimum = Math.Min(minimum,
                                 dp[i - 1, 1]);
           
          // Condition if current element
          // is not equal to previous element
          // after being increased by 2
          if (j + arr[i] != arr[i - 1] + 2)
              minimum = Math.Min(minimum,
                                 dp[i - 1, 2]);
           
          // Take the minimum from all cases
          dp[i, j] = j * cost[i] + minimum;
       }
    }
    int ans = (int) 1e6;
 
    // Finding the minimum cost
    for(int i = 0; i < 3; i++)
       ans = Math.Min(ans, dp[N - 1, i]);
        
    // Printing the minimum cost
    // required to make all adjacent
    // elements disinct
    Console.Write(ans + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 2, 2, 3, 4 };
    int []cost = { 3, 2, 5, 4, 2, 1 };
    int N = arr.Length;
 
    minimumCost(arr, cost, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
7

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live