📌  相关文章
📜  将除 1 以外的所有数组元素减少到 0 所需的对减量最大化

📅  最后修改于: 2021-10-28 02:04:41             🧑  作者: Mango

给定一个由N 个不同元素组成的数组arr[] ,任务是找到每一步需要减少1的最大对数,使得N – 1 个数组元素减少到 0,剩下的数组元素是一个非负整数。

例子:

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

  • 初始化一个变量,比如cntOp ,以存储使数组的(N – 1) 个元素等于0所需的最大步数。
  • 创建一个优先级队列,比如PQ ,来存储数组元素。
  • 遍历数组并将数组元素插入到PQ 中
  • 现在重复从优先级队列中提取前2 个元素,将两个元素的值都减少1 ,再次将两个元素插入优先级队列并将cntOp增加1 。当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)

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