给定一个大小为N的数组arr[]和一个整数K ( N % K = 0) ,任务是找到删除所有数组元素的最大成本。在每次操作中,恰好可以移除K 个数组元素,移除成本等于移除的第二个最小元素。
例子:
Input: arr[] = { 1, 3, 4, 1, 5, 1, 5, 3 }, K = 4
Output: 5
Explanation:
Removing {arr[0], arr[3], arr[5], arr[7]} modifies arr[] to {3, 4, 5, 5}. Second smallest element removed = 1. Therefore, cost = 1
Removing {arr[0], arr[1], arr[2], arr[3]} modifies arr[] to {}. Second smallest element removed = 4. Therefore, cost = 1 + 4 = 5
Therefore, the required output is = 5
Input: arr[] = { 1, 2, 3, 4}, K = 4
Output: 2
方法:该问题可以使用贪心技术解决。这个想法是对数组进行排序,并在每次操作时重复删除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 maximum 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 maximum 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 maximum 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 maximum 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。