通过将元素减少 1 或将最多 K 个元素替换为 0 将 Array 减少为 0
给定一个由N个整数组成的数组arr[]和一个正整数K ,任务是找到将所有数组元素减少到0所需的最小操作数,使得在每个操作中将任何数组元素减少1并且最多独立地减少 K个数组元素可以减少到0 。
例子:
Input: arr[] = {4, 1, 5}, K = 1
Output: 5
Explanation: Following are the operations performed to convert all array elements to 0:
Here K = 1, So replace arr[2] by 0, converts arr[] to {4, 1, 0} –> Number of operations = 0.
Decrease arr[1] by 1, converts arr[] to {4, 0, 0} –> Number of operations = 1.
Decrease arr[0] by 1 four times, converts arr[] to {0, 0, 0} –> Number of operations = 4.
Therefore, total number of operations = 0 + 1 + 4 = 5, which is minimum possible.
Input: arr[] = {4, 2, 10, 9, 18}, K = 2
Output: 15
方法:给定问题可以通过使用贪心方法来解决,该方法基于以下思想:首先对给定数组 arr[] 进行非递减顺序排序,然后将 K 最大元素更新为0 ,并对给定的操作执行剩余的数组元素。请按照以下步骤解决给定的问题:
- 如果N的值最多为K ,则将所有数组元素替换为0 。因此,在这种情况下所需的操作数将为0 。
- 以非递减顺序对数组arr[]进行排序。
- 用0替换最后的K个元素。
- 打印前(N – K)个元素的总和作为操作的结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// operations needed to convert all the
// array elements to 0
int minOperations(vector arr,
int K, int N)
{
// If K is greater then 0 then
// replace all array elements to 0
if (K >= N)
return 0;
// Sort array in non-decreasing order
sort(arr.begin(), arr.end());
// Stores the count of operations
// required
int countOperations = 0;
// Iterate loop until N - K times
for (int i = 0; i < N - K; i++) {
// Take sum of elements
countOperations += arr[i];
}
// Return countOperations as
// the required answer
return countOperations;
}
// Driver Code
int main()
{
vector arr{ 4, 1, 5 };
int N = arr.size();
int K = 1;
cout << minOperations(arr, K, N);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
public class GFG {
// Function to find the minimum number
// operations needed to convert all the
// array elements to 0
static int minOperations(int []arr,
int K, int N)
{
// If K is greater then 0 then
// replace all array elements to 0
if (K >= N)
return 0;
// Sort array in non-decreasing order
Arrays.sort(arr) ;
// Stores the count of operations
// required
int countOperations = 0;
// Iterate loop until N - K times
for (int i = 0; i < N - K; i++) {
// Take sum of elements
countOperations += arr[i];
}
// Return countOperations as
// the required answer
return countOperations;
}
// Driver Code
public static void main (String[] args)
{
int[] arr = { 4, 1, 5 };
int N = arr.length;
int K = 1;
System.out.println(minOperations(arr, K, N));
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find the minimum number
# operations needed to convert all the
# array elements to 0
def minOperations(arr, K, N) :
# If K is greater then 0 then
# replace all array elements to 0
if (K >= N) :
return 0;
# Sort array in non-decreasing order
arr.sort();
# Stores the count of operations
# required
countOperations = 0;
# Iterate loop until N - K times
for i in range(N - K) :
# Take sum of elements
countOperations += arr[i];
# Return countOperations as
# the required answer
return countOperations;
# Driver Code
if __name__ == "__main__" :
arr = [ 4, 1, 5 ];
N = len(arr);
K = 1;
print(minOperations(arr, K, N));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum number
// operations needed to convert all the
// array elements to 0
static int minOperations(int []arr,
int K, int N)
{
// If K is greater then 0 then
// replace all array elements to 0
if (K >= N)
return 0;
// Sort array in non-decreasing order
Array.Sort(arr) ;
// Stores the count of operations
// required
int countOperations = 0;
// Iterate loop until N - K times
for (int i = 0; i < N - K; i++) {
// Take sum of elements
countOperations += arr[i];
}
// Return countOperations as
// the required answer
return countOperations;
}
// Driver Code
public static void Main (string[] args)
{
int[] arr = { 4, 1, 5 };
int N = arr.Length;
int K = 1;
Console.WriteLine(minOperations(arr, K, N));
}
}
// This code is contributed by AnkThon
Javascript
5
时间复杂度: O(N*log N)
辅助空间: O(1)