给定一个由N 个整数组成的数组arr[] ,任务是在以指定的顺序执行以下操作任意次数后,找到删除所有数组元素的成本:
- 将给定数组中存在的最大元素添加到成本中。
- 从给定数组中删除最大元素。
- 将给定数组中的所有整数减1 。
- 删除减少为 0 的元素。
例子:
Input: arr[] = {1, 6, 7, 4, 2, 5, 3}
Output: 16
Explanation:
Step 1: Current maximum element = 7. Removing 7 and reducing each element by 1 modifies the array to {0, 5, 3, 1, 4, 2}. Cost = 7.
Step 2: Current maximum element = 5. Removing 5 and reducing each element by 1 modifies the array to {2, 0, 3, 1}. Cost = 7 + 5 = 12.
Step 3: Current maximum = 3. Removing 3 and reducing each element by 1 modifies the array to {1, 1}. Cost = 12 + 3 = 15
Step 4: Final cost obtained = 15 + 1 = 16
Input: arr[] = {6, 4, 6, 1}
Output: 13
朴素的方法:解决问题的最简单的方法如下:
- 从给定数组中找到最大值
- 从数组中删除后将其添加到成本中。
- 删除最大元素后,将所有剩余的数组元素减少 1。删除所有减少为 0 的元素。
- 重复上述步骤,直到删除所有数组元素。
- 最后,打印cost的值。
时间复杂度: O(N 2 ),其中 N 是给定数组的大小。
辅助空间: O(1)
有效的方法:为了优化上述方法,其思想是观察数组中每个第i 大的元素都添加arr[i] – i次到成本中,其中 i 是每个元素arr[i]的索引。请按照以下步骤解决上述问题:
- 按降序对给定数组进行排序。
- 遍历数组,并添加值ARR [Ⅰ] -我对于每个阵列元素到成本,如果它是大于0。
- 完成上述步骤后,打印cost的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total cost
// of removing all array elements
int findCost(int* a, int n)
{
// Sort the array in descending order
sort(a, a + n, greater());
// Stores the total cost
int count = 0;
for (int j = 0; j < n; j++) {
// Contribution of i-th
// greatest element to the cost
int p = a[j] - j;
// Remove the element
a[j] = 0;
// If negative
if (p < 0) {
p = 0;
continue;
}
// Add to the final cost
count += p;
}
// Return the cost
return count;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 6, 7, 4, 2, 5, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << findCost(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the total cost
// of removing all array elements
static int findCost(Integer [] a, int n)
{
// Sort the array in descending order
Arrays.sort(a, Collections.reverseOrder());
// Stores the total cost
int count = 0;
for (int j = 0; j < n; j++)
{
// Contribution of i-th
// greatest element to the cost
int p = a[j] - j;
// Remove the element
a[j] = 0;
// If negative
if (p < 0)
{
p = 0;
continue;
}
// Add to the final cost
count += p;
}
// Return the cost
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
Integer arr[] = {1, 6, 7,
4, 2, 5, 3};
int N = arr.length;
// Function Call
System.out.print(findCost(arr, N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to find the total cost
# of removing all array elements
def findCost(a, n):
# Sort the array in descending order
a.sort(reverse = True)
# Stores the total cost
count = 0
for j in range(n):
# Contribution of i-th
# greatest element to the cost
p = a[j] - j
# Remove the element
a[j] = 0
# If negative
if(p < 0):
p = 0
continue
# Add to the final cost
count += p
# Return the cost
return count
# Driver Code
# Given array arr[]
arr = [ 1, 6, 7, 4, 2, 5, 3 ]
N = len(arr)
# Function call
print(findCost(arr, N))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the total cost
// of removing all array elements
static int findCost(int []a, int n)
{
// Sort the array in descending order
Array.Sort(a);
Array.Reverse(a);
// Stores the total cost
int count = 0;
for (int j = 0; j < n; j++)
{
// Contribution of i-th
// greatest element to the cost
int p = a[j] - j;
// Remove the element
a[j] = 0;
// If negative
if (p < 0)
{
p = 0;
continue;
}
// Add to the readonly cost
count += p;
}
// Return the cost
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = {1, 6, 7,
4, 2, 5, 3};
int N = arr.Length;
// Function Call
Console.Write(findCost(arr, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
16
时间复杂度: O(NlogN),其中 N 是给定数组的大小。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。