📌  相关文章
📜  通过最小化每个子数组中重复元素的数量,以最低成本将数组拆分为子数组

📅  最后修改于: 2021-09-17 07:12:10             🧑  作者: Mango

给定的阵列ARR []从范围具有N个整数[1,N]和整数K,任务是找到最小可能的成本到阵列分割成可以基于以下条件来实现非空的子阵列:

  • 如果子数组中不存在唯一元素,则成本为K
  • 否则,成本是K + 每个重复元素的频率之和。

例子:

朴素的方法:解决问题的最简单的想法是生成所有可能的子数组来预先计算并存储它们各自的成本。然后,计算可以在阵列上执行的每个可能拆分的成本。最后,打印所有拆分的最小成本。
请按照以下步骤解决问题:

  1. 根据上述条件预先计算每个子阵列的成本。
  2. 生成可以在阵列上执行的所有可能的拆分。
  3. 对于每个拆分,计算每个拆分器子阵列的总成本。
  4. 保持生成的最小总成本,最后打印最小总和。

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

Efficient Approach:思路是使用Dynamic Programming来优化上面的方法。请按照以下步骤解决问题:

  1. 在所有索引处使用 INT_MAX 初始化长度为N的数组dp[]
  2. 初始化数组的第一个元素。
  3. 对于任何索引i ,数组dp[i]表示将数组划分为从0 到 i 的子数组的最小成本。
  4. 对于每个索引i ,计算从i 到 N 的所有索引的最小成本。
  5. 对数组的所有元素重复此过程
  6. 返回dp[]的最后一个元素以获得拆分数组的最小成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#define ll long long
using namespace std;
 
// Function to find the minimum cost
// of splitting the array into subarrays
int findMinCost(vector& a, int k)
{
    // Size of the array
    int n = (int)a.size();
 
    // Get the maximum element
    int max_ele = *max_element(a.begin(),
                               a.end());
 
    // dp[] will store the minimum cost
    // upto index i
    ll dp[n + 1];
 
    // Initialize the result array
    for (int i = 1; i <= n; ++i)
        dp[i] = INT_MAX;
 
    // Initialise the first element
    dp[0] = 0;
 
    for (int i = 0; i < n; ++i) {
 
        // Create the frequency array
        int freq[max_ele + 1];
 
        // Initialize frequency array
        memset(freq, 0, sizeof freq);
 
        for (int j = i; j < n; ++j) {
 
            // Update the frequency
            freq[a[j]]++;
            int cost = 0;
 
            // Counting the cost of
            // the duplicate element
            for (int x = 0;
                 x <= max_ele; ++x) {
                cost += (freq[x] == 1)
                            ? 0
                            : freq[x];
            }
 
            // Minimum cost of operation
            // from 0 to j
            dp[j + 1] = min(dp[i] + cost + k,
                            dp[j + 1]);
        }
    }
 
    // Total cost of the array
    return dp[n];
}
 
// Driver Code
int main()
{
    vector arr = { 1, 2, 1, 1, 1 };
 
    // Given cost K
    int K = 2;
 
    // Function Call
    cout << findMinCost(arr, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
// Function to find the
// minimum cost of splitting
// the array into subarrays
static long findMinCost(int[] a,
                        int k, int n)
{
  // Get the maximum element
  int max_ele = Arrays.stream(a).max().getAsInt();
 
  // dp[] will store the minimum cost
  // upto index i
  long[] dp = new long[n + 1];
 
  // Initialize the result array
  for (int i = 1; i <= n; ++i)
    dp[i] = Integer.MAX_VALUE;
 
  // Initialise the first element
  dp[0] = 0;
 
  for (int i = 0; i < n; ++i)
  {
    // Create the frequency array
    int[] freq = new int[max_ele + 1];
 
    for (int j = i; j < n; ++j)
    {
      // Update the frequency
      freq[a[j]]++;
      int cost = 0;
 
      // Counting the cost of
      // the duplicate element
      for (int x = 0; x <= max_ele; ++x)
      {
        cost += (freq[x] == 1) ? 0 :
                 freq[x];
      }
 
      // Minimum cost of operation
      // from 0 to j
      dp[j + 1] = Math.min(dp[i] + cost + k,
                           dp[j + 1]);
    }
  }
 
  // Total cost of the array
  return dp[n];
}
 
// Driver Code
public static void main(String[] args)
{
  int[] arr = {1, 2, 1, 1, 1};
 
  // Given cost K
  int K = 2;
  int n = arr.length;
   
  // Function Call
  System.out.print(findMinCost(arr,
                               K, n));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
import sys
 
# Function to find the
# minimum cost of splitting
# the array into subarrays
def findMinCost(a, k, n):
     
    # Get the maximum element
    max_ele = max(a)
 
    # dp will store the minimum cost
    # upto index i
    dp = [0] * (n + 1)
 
    # Initialize the result array
    for i in range(1, n + 1):
        dp[i] = sys.maxsize
 
    # Initialise the first element
    dp[0] = 0
 
    for i in range(0, n):
         
        # Create the frequency array
        freq = [0] * (max_ele + 1)
 
        for j in range(i, n):
             
            # Update the frequency
            freq[a[j]] += 1
            cost = 0
 
            # Counting the cost of
            # the duplicate element
            for x in range(0, max_ele + 1):
                cost += (0 if (freq[x] == 1) else
                               freq[x])
 
            # Minimum cost of operation
            # from 0 to j
            dp[j + 1] = min(dp[i] + cost + k,
                            dp[j + 1])
 
    # Total cost of the array
    return dp[n]
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 1, 1, 1 ];
 
    # Given cost K
    K = 2;
    n = len(arr);
 
    # Function call
    print(findMinCost(arr, K, n));
 
# This code is contributed by Amit Katiyar


C#
// C# program for the above approach
using System;
using System.Linq;
class GFG{
 
// Function to find the
// minimum cost of splitting
// the array into subarrays
static long findMinCost(int[] a,
                        int k, int n)
{
  // Get the maximum element
  int max_ele = a.Max();
 
  // []dp will store the minimum cost
  // upto index i
  long[] dp = new long[n + 1];
 
  // Initialize the result array
  for (int i = 1; i <= n; ++i)
    dp[i] = int.MaxValue;
 
  // Initialise the first element
  dp[0] = 0;
 
  for (int i = 0; i < n; ++i)
  {
    // Create the frequency array
    int[] freq = new int[max_ele + 1];
 
    for (int j = i; j < n; ++j)
    {
      // Update the frequency
      freq[a[j]]++;
      int cost = 0;
 
      // Counting the cost of
      // the duplicate element
      for (int x = 0; x <= max_ele; ++x)
      {
        cost += (freq[x] == 1) ? 0 :
                 freq[x];
      }
 
      // Minimum cost of operation
      // from 0 to j
      dp[j + 1] = Math.Min(dp[i] + cost + k,
                           dp[j + 1]);
    }
  }
 
  // Total cost of the array
  return dp[n];
}
 
// Driver Code
public static void Main(String[] args)
{
  int[] arr = {1, 2, 1, 1, 1};
 
  // Given cost K
  int K = 2;
  int n = arr.Length;
   
  // Function Call
  Console.Write(findMinCost(arr,
                               K, n));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
6

时间复杂度: O(N 3 ),其中 N 是给定数组的大小。
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程