📌  相关文章
📜  最大限度地减少将所有数组元素(从1减少到0)所需的对减量

📅  最后修改于: 2021-05-17 05:38:41             🧑  作者: Mango

给定一个由N个不同元素组成的数组arr [] ,任务是查找每个步骤中需要减少1对的最大对数,以使N – 1个数组元素减少为0,而其余的数组元素为a。非负整数。

例子:

处理方法:可以贪婪地解决问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntOp ,以存储使数组的(N – 1)个元素等于0所需的最大步数。
  • 创建一个优先级队列,例如PQ ,以存储数组元素。
  • 遍历数组并将数组元素插入PQ
  • 现在,从优先级队列中重复提取前2个元素,将这两个元素的值都减1 ,再次将这两个元素插入优先级队列,并将cntOp1 。当PQ的(N – 1)元素等于0时,此过程继续进行。
  • 最后,打印cntOp的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count maximum number of steps
// to make (N - 1) array elements to 0
int cntMaxOperationToMakeN_1_0(int arr[], int N)
{
 
    // Stores maximum count of steps to make
    // (N - 1) elements equal to 0
    int cntOp = 0;
 
    // Stores array elements
    priority_queue PQ;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Insert arr[i] into PQ
        PQ.push(arr[i]);
    }
 
    // Extract top 2 elements from the array
    // while (N - 1) array elements become 0
    while (PQ.size() > 1) {
 
        // Stores top element
        // of PQ
        int X = PQ.top();
 
        // Pop the top element
        // of PQ.
        PQ.pop();
 
        // Stores top element
        // of PQ
        int Y = PQ.top();
 
        // Pop the top element
        // of PQ.
        PQ.pop();
 
        // Update X
        X--;
 
        // Update Y
        Y--;
 
        // If X is not equal to 0
        if (X != 0) {
 
            // Insert X into PQ
            PQ.push(X);
        }
 
        // if Y is not equal
        // to 0
        if (Y != 0) {
 
            // Insert Y
            // into PQ
            PQ.push(Y);
        }
 
        // Update cntOp
        cntOp += 1;
    }
 
    return cntOp;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 3, 4, 5 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << cntMaxOperationToMakeN_1_0(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Function to count maximum number of steps
// to make (N - 1) array elements to 0
static int cntMaxOperationToMakeN_1_0(int[] arr, int N)
{
     
    // Stores maximum count of steps to make
    // (N - 1) elements equal to 0
    int cntOp = 0;
     
    // Stores array elements
    PriorityQueue PQ = new PriorityQueue((a, b) -> b - a);
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Insert arr[i] into PQ
        PQ.add(arr[i]);
    }
     
    // Extract top 2 elements from the array
    // while (N - 1) array elements become 0
    while (PQ.size() > 1)
    {
         
        // Stores top element
        // of PQ
        int X = PQ.peek();
         
        // Pop the top element
        // of PQ.
        PQ.remove();
         
        // Stores top element
        // of PQ
        int Y = PQ.peek();
         
        // Pop the top element
        // of PQ.
        PQ.remove();
         
        // Update X
        X--;
         
        // Update Y
        Y--;
         
        // If X is not equal to 0
        if (X != 0)
        {
             
            // Insert X into PQ
            PQ.add(X);
        }
         
        // if Y is not equal
        // to 0
        if (Y != 0)
        {
             
            // Insert Y
            // into PQ
            PQ.add(Y);
        }
         
        // Update cntOp
        cntOp += 1;
    }
    return cntOp;
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.length;
 
    System.out.print(cntMaxOperationToMakeN_1_0(arr, N));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Function to count maximum number of steps
# to make (N - 1) array elements to 0
def cntMaxOperationToMakeN_1_0(arr, N):
 
    # Stores maximum count of steps to make
    # (N - 1) elements equal to 0
    cntOp = 0
 
    # Stores array elements
    PQ = []
 
    # Traverse the array
    for i in range(N):
 
        # Insert arr[i] into PQ
        PQ.append(arr[i])
    PQ = sorted(PQ)
 
    # Extract top 2 elements from the array
    # while (N - 1) array elements become 0
    while (len(PQ) > 1):
 
        # Stores top element
        # of PQ
        X = PQ[-1]
 
        # Pop the top element
        # of PQ.
        del PQ[-1]
 
        # Stores top element
        # of PQ
        Y = PQ[-1]
 
        # Pop the top element
        # of PQ.
        del PQ[-1]
 
        # Update X
        X -= 1
 
        # Update Y
        Y -= 1
 
        # If X is not equal to 0
        if (X != 0):
 
            # Insert X into PQ
            PQ.append(X)
 
        # if Y is not equal
        # to 0
        if (Y != 0):
 
            # Insert Y
            # into PQ
            PQ.append(Y)
 
        # Update cntOp
        cntOp += 1
        PQ = sorted(PQ)
    return cntOp
 
# Driver Code
if __name__ == '__main__':
 
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
    print (cntMaxOperationToMakeN_1_0(arr, N))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to count maximum number of steps
  // to make (N - 1) array elements to 0
  static int cntMaxOperationToMakeN_1_0(int[] arr, int N)
  {
 
    // Stores maximum count of steps to make
    // (N - 1) elements equal to 0
    int cntOp = 0;
 
    // Stores array elements
    List PQ = new List();
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Insert arr[i] into PQ
      PQ.Add(arr[i]);
    }
 
    PQ.Sort();
    PQ.Reverse();
 
    // Extract top 2 elements from the array
    // while (N - 1) array elements become 0
    while (PQ.Count > 1) {
 
      // Stores top element
      // of PQ
      int X = PQ[0];
 
      // Pop the top element
      // of PQ.
      PQ.RemoveAt(0);
 
      // Stores top element
      // of PQ
      int Y = PQ[0];
 
      // Pop the top element
      // of PQ.
      PQ.RemoveAt(0);
 
      // Update X
      X--;
 
      // Update Y
      Y--;
 
      // If X is not equal to 0
      if (X != 0) {
 
        // Insert X into PQ
        PQ.Add(X);
        PQ.Sort();
        PQ.Reverse();
      }
 
      // if Y is not equal
      // to 0
      if (Y != 0) {
 
        // Insert Y
        // into PQ
        PQ.Add(Y);
        PQ.Sort();
        PQ.Reverse();
      }
 
      // Update cntOp
      cntOp += 1;
    }
 
    return cntOp;
  }
 
  // Driver code
  static void Main() {
    int[] arr = { 1, 2, 3, 4, 5 };
 
    int N = arr.Length;
 
    Console.WriteLine(cntMaxOperationToMakeN_1_0(arr, N));
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:
7

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