给定一个由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)
高效方法:为了优化上述方法,思想是使用动态规划,因为上述递推关系存在重叠的子问题和重叠的子结构。请按照以下步骤解决问题:
- 用一些大值初始化一个大小为N*2N的二维数组cost[][] ,其中cost[i][j]表示使用j从给定数组中删除直到第i个索引的所有元素的最小成本多少时间。
- 此外,将cost[0][0]初始化为0和变量, prev为0 ,其中prev将存储先前索引的所有先前成本值中的最小值。
- 使用变量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 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live