📌  相关文章
📜  通过重复删除给定操作获得的最大值来清空给定数组所需的成本

📅  最后修改于: 2021-04-22 02:12:28             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到以指定顺序执行以下操作多次后删除所有数组元素的开销:

  • 将给定数组中存在的最大元素添加到成本中。
  • 从给定数组中删除最大元素。
  • 将给定数组中的所有整数减少1
  • 删除缩小为0的元素。

例子:

天真的方法:解决问题的最简单方法如下:

  • 从给定数组中找到最大值
  • 从阵列中删除后,将其添加到成本中
  • 删除最大元素后,将所有剩余的数组元素减少1。删除所有减小为0的元素。
  • 重复上述步骤,直到删除所有数组元素。
  • 最后,打印cost的值。

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

高效方法:为了优化上述方法,我们的想法是观察到数组中的每个i最大元素都增加了arr [i] – i倍于成本,其中i是每个元素arr [i]的索引。请按照以下步骤解决以上问题:

  1. 以降序对给定数组进行排序。
  2. 遍历数组,并为每个数组元素添加值arr [i] – i (如果该元素大于0则要花费)
  3. 完成上述步骤后,打印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


输出:
16




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