📌  相关文章
📜  最小化连续删除相同类型的元素以清空给定数组

📅  最后修改于: 2021-10-27 06:17:19             🧑  作者: Mango

给定一个由N 个整数组成的数组A[ ] ,这样每个数组元素A i表示第i类型元素的计数,任务是最小化相同类型连续元素的删除次数以清空给定数组.

例子:

方法:该问题可以通过Greedy Approach解决。请按照以下步骤解决问题:

  • 对给定的数组进行排序。
  • 计算数组元素的总和。
  • 找出数组中存在的最大元素,即A N-1。 .
  • 如果总和和最大元素之间的差大于最大元素,则打印 0 作为所需答案。
  • 否则,打印2* max – sum -1作为所需答案。

下面是上述方法的实现。

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to count minimum consecutive
// removals of elements of the same type
void minRemovals(int A[], int N)
{
  
    // Sort the array
    sort(A, A + N);
  
    // Stores the maximum element
    // present in the array
    int mx = A[N - 1];
  
    // Stores sum of the array
    int sum = 1;
  
    // Calculate sum of the array
    for (int i = 0; i < N; i++) {
        sum += A[i];
    }
  
    if (sum - mx >= mx) {
        cout << 0 << "\n";
    }
    else {
        cout << 2 * mx - sum << "\n";
    }
}
  
// Driver Code
int main()
{
    int A[] = { 3, 3, 2 };
    int N = sizeof(A) / sizeof(A[0]);
  
    // Function call
    minRemovals(A, N);
    return 0;
}


Java
// Java implementation of the above approach
import java.util.Arrays;
  
class GFG 
{
      
    // Function to count minimum consecutive
    // removals of elements of the same type
    static void minRemovals(int []A, int N)
    {
      
        // Sort the array
        Arrays.sort(A);
      
        // Stores the maximum element
        // present in the array
        int mx = A[N - 1];
      
        // Stores sum of the array
        int sum = 1;
      
        // Calculate sum of the array
        for (int i = 0; i < N; i++)
        {
            sum += A[i];
        }
      
        if (sum - mx >= mx) {
            System.out.println(0);
        }
        else {
            System.out.println(2 * mx - sum);
        }
    }
      
    // Driver Code
    public static void main(String[] args) {
          
        int []A = { 3, 3, 2 };
        int N = A.length;
      
        // Function call
        minRemovals(A, N);
          
    }
}
  
// This code is contributed by AnkThon


Python3
# Python3 implementation of the above approach
  
# Function to count minimum consecutive 
# removals of elements of the same type 
def minRemovals(A, N):
      
    # Sort the array
    A.sort()
  
    # Stores the maximum element 
    # present in the array 
    mx = A[N - 1]
  
    # stores the sum of array
    sum = 1
  
    # Calculate sum of array
    for i in range(0, N):
        sum += A[i]
  
    if ((sum - mx) >= mx):
        print(0, end = "")
    else:
        print(2 * mx - sum, end = "")
  
# Driver Code  
if __name__ == "__main__" :
      
    A = [ 3, 3, 2 ]
    N = len(A)
      
    # Function call 
    minRemovals(A, N) 
  
# This code is contributed by Virusbuddah


C#
// C# implementation of the above approach
using System;
  
class GFG 
{
      
    // Function to count minimum consecutive
    // removals of elements of the same type
    static void minRemovals(int []A, int N)
    {
      
        // Sort the array
        Array.Sort(A);
      
        // Stores the maximum element
        // present in the array
        int mx = A[N - 1];
      
        // Stores sum of the array
        int sum = 1;
      
        // Calculate sum of the array
        for (int i = 0; i < N; i++)
        {
            sum += A[i];
        }
      
        if (sum - mx >= mx) 
        {
            Console.WriteLine(0);
        }
        else
        {
            Console.WriteLine(2 * mx - sum);
        }
    }
      
    // Driver Code
    public static void Main(String[] args) 
    {
        int []A = { 3, 3, 2 };
        int N = A.Length;
      
        // Function call
        minRemovals(A, N);      
    }
}
  
// This code is contributed by shikhasingrajput


Javascript


输出
0

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程