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

📅  最后修改于: 2021-05-06 10:05:45             🧑  作者: Mango

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

例子:

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

  • 对给定的数组进行排序。
  • 计算数组元素的总和。
  • 查找数组中存在的最大元素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


输出
0

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