📌  相关文章
📜  最小化递减以使每个 Array 元素相同或 0

📅  最后修改于: 2022-05-13 01:56:10.496000             🧑  作者: Mango

最小化递减以使每个 Array 元素相同或 0

给定一个由N个正整数组成的数组arr[] 。在一次操作中,任何数量的数组都可以减 1。任务是找到使所有元素等于或 0 所需的最小操作数。

例子

方法:可以使用以下想法通过贪心方法解决问题:

请按照以下步骤解决问题:

  • 对给定的数组进行排序。
  • 计算所有数组元素的总和并将其存储在一个变量中,例如total_sum
  • 创建一个变量minimum_operation = INT_MAX ,运行一个循环并更新minimum_operation = min(minimum_operation, total_sum – (N – i)*arr[i])
  • 返回minimum_operation作为答案。

下面是上述方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find minimum operation
int minimumRemoval(int arr[], int n)
{
 
    // Variable to store sum of array
    int total_sum = 0;
 
    // Variable to store answer
    int minimum_operation = INT_MAX;
 
    // Sort the given array
    sort(arr, arr + n);
 
    for (int index = 0; index < n; index++) {
        total_sum += arr[index];
    }
 
    // Find minimum operation by keeping
    // each array  value as the remaining
    // elements value
    for (int index = 0; index < n; index++) {
 
        int curr_operation
            = total_sum
              - ((n - index) * arr[index]);
 
        if (curr_operation < minimum_operation) {
            minimum_operation = curr_operation;
        }
    }
 
    // Return minimum_operation
    return minimum_operation;
}
 
// Driver code
int main()
{
    int arr[4] = { 4, 1, 6, 6 };
    int N = 4;
 
    // Function call
    cout << minimumRemoval(arr, N);
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
 
class GFG{
 
// Function to find minimum operation
static int minimumRemoval(int arr[], int n)
{
 
    // Variable to store sum of array
    int total_sum = 0;
 
    // Variable to store answer
    int minimum_operation = Integer.MAX_VALUE;
 
    // Sort the given array
    Arrays.sort(arr);
 
    for (int index = 0; index < n; index++) {
        total_sum += arr[index];
    }
 
    // Find minimum operation by keeping
    // each array  value as the remaining
    // elements value
    for (int index = 0; index < n; index++) {
 
        int curr_operation
            = total_sum
              - ((n - index) * arr[index]);
 
        if (curr_operation < minimum_operation) {
            minimum_operation = curr_operation;
        }
    }
 
    // Return minimum_operation
    return minimum_operation;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 1, 6, 6 };
    int N = 4;
 
    // Function call
    System.out.print(minimumRemoval(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code to implement the approach
import sys
 
# Function to find minimum operation
def minimumRemoval(arr, n) :
 
    # Variable to store sum of array
    total_sum = 0
 
    # Variable to store answer
    minimum_operation = sys.maxsize
 
    # Sort the given array
    arr.sort()
 
    for index in range(0, n) :
        total_sum += arr[index]
     
 
    # Find minimum operation by keeping
    # each array value as the remaining
    # elements value
    for index in range(0, n) :
 
        curr_operation = (total_sum
            - ((n - index) * arr[index]))
 
        if (curr_operation < minimum_operation) :
            minimum_operation = curr_operation
         
     
 
    # Return minimum_operation
    return minimum_operation
 
 
# Driver code
 
arr = [ 4, 1, 6, 6 ]
N = 4
 
# Function call
print(minimumRemoval(arr, N))
 
# This code is contributed by code_hunt.


C#
// C# code to implement the approach
using System;
class GFG {
 
  // Function to find minimum operation
  static int minimumRemoval(int[] arr, int n)
  {
 
    // Variable to store sum of array
    int total_sum = 0;
 
    // Variable to store answer
    int minimum_operation = Int32.MaxValue;
 
    // Sort the given array
    Array.Sort(arr);
 
    for (int index = 0; index < n; index++) {
      total_sum += arr[index];
    }
 
    // Find minimum operation by keeping
    // each array  value as the remaining
    // elements value
    for (int index = 0; index < n; index++) {
 
      int curr_operation
        = total_sum - ((n - index) * arr[index]);
 
      if (curr_operation < minimum_operation) {
        minimum_operation = curr_operation;
      }
    }
 
    // Return minimum_operation
    return minimum_operation;
  }
 
  // Driver code
  public static void Main()
  {
    int[] arr = { 4, 1, 6, 6 };
    int N = 4;
 
    // Function call
    Console.Write(minimumRemoval(arr, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
5

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