给定一个由N个整数组成的数组arr [] ,任务是找到从数组中删除所有元素的最小开销,以使删除任何元素的开销为当前时刻T (最初为1 )与指针之间的绝对差。数组元素arr [i],即abs(T – arr [i]) ,其中T。
例子:
Input: arr[] = {3, 6, 4, 2}
Output: 0
Explanation:
T = 1: No removal
T = 2: Remove arr[3]. Cost = |2 – 2| = 0
T = 3: Remove arr[0]. Cost = |3 – 3| = 0
T = 4: Remove arr[2]. Cost = |4 – 4| = 0
T = 5: No removal.
T = 6: Remove arr[1]. Cost = |0| + |6 – 6| = 0
Therefore, the total cost = 0
Input: arr[] = {4, 2, 4, 4, 5, 2}
Output: 4
天真的方法:这个想法是使用递归来解决问题。在每个时刻,都存在两种可能性,即是否删除任何元素。因此,为了最大程度地降低成本,请对数组进行排序。然后,从索引0和时间T = 1开始,使用以下递归关系解决问题:
minCost(index, time) = min(minCost(index + 1, T + 1) + abs(time – a[index]), minCost(index, T + 1))
where, Base Case: If current index exceeds the current size of the array.
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用动态编程,因为上述递归关系存在重叠的子问题和重叠的子结构。请按照以下步骤解决问题:
- 初始化一个2-d阵列成本[[]的大小,] N * 2N一些较大的值,其中成本[i] [j]表示删除所有的元素,直到第i从使用j个给定的数组个索引的最小成本多少时间。
- 此外,初始化成本[0] [0] 0和可变的,先前与0其中一个先前将存储的最小前一个索引的所有先前的成本值的。
- 使用变量i遍历给定数组arr [] ,然后对于每个i ,使用变量j迭代[1,2N]范围:
- 如果(prev + abs(j – arr [i – 1])的值小于cost [i] [j] ,则将cost [i] [j]更新为该值。
- 如果cost [i – 1] [j]小于prev ,则将prev更新为该值。
- 完成上述步骤后,将最小成本打印为cost [N] [j] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define INF 10000
// Function to find the minimum cost
// to delete all array elements
void minCost(int arr[], int n)
{
// Sort the input array
sort(arr, arr + n);
// Store the maximum time to delete
// the array in the worst case
int m = 2 * n;
// Store the result in cost[][] table
int cost[n + 1][m + 1];
// Initialize the table cost[][]
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
cost[i][j] = INF;
}
}
// Base Case
cost[0][0] = 0;
// Store the minimum of all cost
// values of the previous index
int prev = 0;
// Iterate from range [1, n]
// using variable i
for (int i = 1; i <= n; i++) {
// Update prev
prev = cost[i - 1][0];
// Iterate from range [1, m]
// using variable j
for (int j = 1; j <= m; j++) {
// Update cost[i][j]
cost[i][j] = min(cost[i][j],
prev
+ abs(j - arr[i - 1]));
// Update the prev
prev = min(prev, cost[i - 1][j]);
}
}
// Store the minimum cost to
// delete all elements
int minCost = INF;
// Find the minimum of all values
// of cost[n][j]
for (int j = 1; j <= m; j++) {
minCost = min(minCost, cost[n][j]);
}
// Print minimum cost
cout << minCost;
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 4, 4, 5, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
minCost(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
static int INF = 10000;
// Function to find the minimum cost
// to delete all array elements
static void minCost(int arr[], int n)
{
// Sort the input array
Arrays.sort(arr);
// Store the maximum time to delete
// the array in the worst case
int m = 2 * n;
// Store the result in cost[][] table
int cost[][] = new int[n + 1][m + 1];
// Initialize the table cost[][]
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= m; j++)
{
cost[i][j] = INF;
}
}
// Base Case
cost[0][0] = 0;
// Store the minimum of all cost
// values of the previous index
int prev = 0;
// Iterate from range [1, n]
// using variable i
for(int i = 1; i <= n; i++)
{
// Update prev
prev = cost[i - 1][0];
// Iterate from range [1, m]
// using variable j
for(int j = 1; j <= m; j++)
{
// Update cost[i][j]
cost[i][j] = Math.min(cost[i][j],
prev + Math.abs(
j - arr[i - 1]));
// Update the prev
prev = Math.min(prev, cost[i - 1][j]);
}
}
// Store the minimum cost to
// delete all elements
int minCost = INF;
// Find the minimum of all values
// of cost[n][j]
for(int j = 1; j <= m; j++)
{
minCost = Math.min(minCost, cost[n][j]);
}
// Print minimum cost
System.out.print(minCost);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 2, 4, 4, 5, 2 };
int N = arr.length;
// Function Call
minCost(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
INF = 10000
# Function to find the minimum cost
# to delete all array elements
def minCost(arr, n):
# Sort the input array
arr = sorted(arr)
# Store the maximum time to delete
# the array in the worst case
m = 2 * n
# Store the result in cost[][] table
cost = [[INF for i in range(m + 1)] for i in range(n + 1)]
# Base Case
cost[0][0] = 0
# Store the minimum of all cost
# values of the previous index
prev = 0
# Iterate from range [1, n]
# using variable i
for i in range(1, n + 1):
# Update prev
prev = cost[i - 1][0]
# Iterate from range [1, m]
# using variable j
for j in range(1, m + 1):
# Update cost[i][j]
cost[i][j] = min(cost[i][j], prev + abs(j - arr[i - 1]))
# Update the prev
prev = min(prev, cost[i - 1][j])
# Store the minimum cost to
# delete all elements
minCost = INF
# Find the minimum of all values
# of cost[n][j]
for j in range(1, m + 1):
minCost = min(minCost, cost[n][j])
# Print minimum cost
print(minCost)
# Driver Code
if __name__ == '__main__':
arr=[4, 2, 4, 4, 5, 2]
N = len(arr)
# Function Call
minCost(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static int INF = 10000;
// Function to find the minimum cost
// to delete all array elements
static void minCost(int[] arr, int n)
{
// Sort the input array
Array.Sort(arr);
// Store the maximum time to delete
// the array in the worst case
int m = 2 * n;
// Store the result in cost[][] table
int[,] cost = new int[n + 1, m + 1];
// Initialize the table cost[][]
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= m; j++)
{
cost[i, j] = INF;
}
}
// Base Case
cost[0, 0] = 0;
// Store the minimum of all cost
// values of the previous index
int prev = 0;
// Iterate from range [1, n]
// using variable i
for(int i = 1; i <= n; i++)
{
// Update prev
prev = cost[i - 1, 0];
// Iterate from range [1, m]
// using variable j
for(int j = 1; j <= m; j++)
{
// Update cost[i][j]
cost[i, j] = Math.Min(cost[i, j],
prev + Math.Abs(
j - arr[i - 1]));
// Update the prev
prev = Math.Min(prev, cost[i - 1, j]);
}
}
// Store the minimum cost to
// delete all elements
int minCost = INF;
// Find the minimum of all values
// of cost[n][j]
for(int j = 1; j <= m; j++)
{
minCost = Math.Min(minCost, cost[n, j]);
}
// Print minimum cost
Console.Write(minCost);
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 2, 4, 4, 5, 2 };
int N = arr.Length;
// Function Call
minCost(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Javascript
4
时间复杂度: O(N 2 )
辅助空间: O(N 2 )