给定一个数组arr [],任务是找到从数组中删除所有元素的最小开销,其中删除元素的开销为2 ^ j * arr [i]。在此,j是已经删除的元素数。
例子:
Input: arr[] = {3, 1, 3, 2}
Output: 25
Explanation:
First remove 3. Cost = 2^(0)*3 = 3
Then remove 3. Cost = 2^(1)*3 = 6
Then remove 2. Cost = 2^(2)*2 = 8
At last, remove 1. Cost = 2^(3)*1 = 8
Total Cost = 3 + 6 + 8 + 8 = 25
Input: arr[] = {1, 2}
Output: 4
Explanation:
First remove 2. Cost = 2^(0)*2 = 2
Then remove 1. Cost = 2^(1)*1 = 2
Total Cost = 2 + 2 = 4
方法:这个想法是使用贪婪的编程范例来解决这个问题。
我们必须最小化表达式(2 ^ j * arr [i])。这可以通过以下方式完成:
- 以降序对数组进行排序。
- 将pow(2,i)乘以每个元素,i从0开始直到数组的大小。
因此,从数组中删除元素的总成本为:
当数组降序排列时。
下面是上述方法的实现:
C++
// C++ implementation to find the
// minimum cost of removing all
// elements from the array
#include
using namespace std;
#define ll long long int
// Function to find the minimum
// cost of removing elements from
// the array
int removeElements(ll arr[], int n)
{
// Sorting in Increasing order
sort(arr, arr + n, greater());
ll ans = 0;
// Loop to find the minimum
// cost of removing elements
for (int i = 0; i < n; i++) {
ans += arr[i] * pow(2, i);
}
return ans;
}
// Driver Code
int main()
{
int n = 4;
ll arr[n] = { 3, 1, 2, 3 };
// Function Call
cout << removeElements(arr, n);
}
Java
// Java implementation to find the
// minimum cost of removing all
// elements from the array
import java.util.*;
class GFG{
// Reverse array in decreasing order
static long[] reverse(long a[])
{
int i, n = a.length;
long t;
for(i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
// Function to find the minimum
// cost of removing elements from
// the array
static long removeElements(long arr[],
int n)
{
// Sorting in Increasing order
Arrays.sort(arr);
arr = reverse(arr);
long ans = 0;
// Loop to find the minimum
// cost of removing elements
for(int i = 0; i < n; i++)
{
ans += arr[i] * Math.pow(2, i);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
long arr[] = { 3, 1, 2, 3 };
// Function call
System.out.print(removeElements(arr, n));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation to find the
# minimum cost of removing all
# elements from the array
# Function to find the minimum
# cost of removing elements from
# the array
def removeElements(arr, n):
# Sorting in Increasing order
arr.sort(reverse = True)
ans = 0
# Loop to find the minimum
# cost of removing elements
for i in range(n):
ans += arr[i] * pow(2, i)
return ans
# Driver Code
if __name__ == "__main__":
n = 4
arr = [ 3, 1, 2, 3 ]
# Function call
print(removeElements(arr, n))
# This code is contributed by chitranayal
C#
// C# implementation to find the
// minimum cost of removing all
// elements from the array
using System;
class GFG{
// Reverse array in decreasing order
static long[] reverse(long []a)
{
int i, n = a.Length;
long t;
for(i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
// Function to find the minimum
// cost of removing elements from
// the array
static long removeElements(long []arr,
int n)
{
// Sorting in Increasing order
Array.Sort(arr);
arr = reverse(arr);
long ans = 0;
// Loop to find the minimum
// cost of removing elements
for(int i = 0; i < n; i++)
{
ans += (long)(arr[i] * Math.Pow(2, i));
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int n = 4;
long []arr = { 3, 1, 2, 3 };
// Function call
Console.Write(removeElements(arr, n));
}
}
// This code is contributed by amal kumar choubey
输出
25
时间复杂度:O(N * log N)
辅助空间:O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。