📌  相关文章
📜  通过重复删除K个数组元素来最大化清空给定数组的成本

📅  最后修改于: 2021-05-13 23:15:51             🧑  作者: Mango

给定一个大小为N且整数K (N%K = 0)的arr []数组,任务是找到删除所有数组元素的最大成本。在每个操作中,可以精确删除K个数组元素,删除成本等于删除的第二个最小元素。

例子:

方法:可以使用贪婪技术解决问题。这个想法是对数组进行排序,并在每次操作中重复删除K个最小的数组元素。请按照以下步骤解决问题:

  • 初始化一个变量,例如maxCost ,以存储删除所有数组元素的最大开销。
  • 以升序对数组进行排序。
  • 使用变量i遍历数组,并更新maxCost = arr [i + 1]i = i + K的值
  • 最后,输出maxCost的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the maximu cost to
// remove all array elements
int maxCostToRemove(int arr[], int N,
                             int K)
{
     
    // Stores maximum cost to
    // remove array elements
    int maxCost = 0;
     
     
    // Sort array in
    // ascending order   
    sort(arr, arr + N);
     
    // Traverse the array
    for (int i = 0; i < N;
                i += K) {
         
        // Update maxCost
        maxCost += arr[i + 1];
    }
     
    return maxCost;
}
 
// Driver Code
int main()
{
    int arr[]= { 1, 3, 4, 1, 5, 1, 5, 3};
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 4;
    cout<< maxCostToRemove(arr, N, K);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
   
class GFG{
   
// Function to find the maximu cost to
// remove all array elements
static int maxCostToRemove(int arr[], int N,
                             int K)
{
      
    // Stores maximum cost to
    // remove array elements
    int maxCost = 0;
      
      
    // Sort array in
    // ascending order   
    Arrays.sort(arr);
      
    // Traverse the array
    for (int i = 0; i < N;
                i += K) {
          
        // Update maxCost
        maxCost += arr[i + 1];
    }
      
    return maxCost;
}
   
// Drive Code
public static void main(String[] args)
{
    int arr[]= { 1, 3, 4, 1, 5, 1, 5, 3};
    int N = arr.length;
    int K = 4;
    System.out.print(maxCostToRemove(arr, N, K));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximu cost to
# remove all array elements
def maxCostToRemove(arr, N, K):
 
    # Stores maximum cost to
    # remove array elements
    maxCost = 0
 
    # Sort array in
    # ascending order
    arr = sorted(arr)
 
    # Traverse the array
    for i in range(0, N, K):
         
        # Update maxCost
        maxCost += arr[i + 1]
 
    return maxCost
 
# Driver Code
if __name__ == '__main__':
    arr= [1, 3, 4, 1, 5, 1, 5, 3]
    N = len(arr)
    K = 4
    print(maxCostToRemove(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
   
// Function to find the maximu cost to
// remove all array elements
static int maxCostToRemove(int []arr, int N,
                             int K)
{
      
    // Stores maximum cost to
    // remove array elements
    int maxCost = 0;
      
      
    // Sort array in
    // ascending order   
    Array.Sort(arr);
      
    // Traverse the array
    for (int i = 0; i < N;
                i += K) {
          
        // Update maxCost
        maxCost += arr[i + 1];
    }
      
    return maxCost;
}
   
// Drive Code
public static void Main(String[] args)
{
    int []arr= { 1, 3, 4, 1, 5, 1, 5, 3};
    int N = arr.Length;
    int K = 4;
    Console.Write(maxCostToRemove(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
5

时间复杂度: O(N * log N)
辅助空间O(1)