📜  每次访问后最大值减少时数组中的最大值

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

给定一个整数K和一个整数数组arr ,任务是从数组中找到最大的元素,每次检索后,该数字将减少1 。重复这些步骤准确K次并打印最后检索到的所有值的总和。

例子:

方法:主要思想是使用最大堆,它在任何时间实例的根都将具有最大元素。

  • 创建数组所有元素的最大堆。
  • 获取堆的根元素并将其添加到总和中。
  • 弹出根元素并将其减1,然后再次将其插入堆中。
  • 重复以上两步正好K次。
  • 最后打印总和。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
#define ll long long
 
ll getSum(int arr[], int K, int n)
{
    ll sum = 0;
    priority_queue maxHeap;
    for (ll i = 0; i < n; i++) {
 
        // put all array elements
        // in a max heap
        maxHeap.push(arr[i]);
    }
 
    while (K--) {
 
        // Get the current maximum element
        ll currentMax = maxHeap.top();
 
        // Add it to the sum
        sum += currentMax;
 
        // Remove the current max from the heap
        maxHeap.pop();
 
        // Add the current max back to the
        // heap after decrementing it by 1
        maxHeap.push(currentMax - 1);
    }
    return sum;
}
 
// driver code
int main()
{
    int arr[] = { 2, 3, 5, 4 }, K = 3;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << getSum(arr, K, n) << endl;
}


Java
// Java implementation of above approach
import java.util.*;
class Solution
{
 
static int getSum(int arr[], int K, int n)
{
    int sum = 0;
    PriorityQueue maxHeap =
                          new PriorityQueue(n,Collections.reverseOrder());
    for (int i = 0; i < n; i++) {
 
        // put aint array elements
        // in a max heap
        maxHeap.add(arr[i]);
    }
 
    while (K-->0) {
 
        // Get the current maximum element
        int currentMax = (int)maxHeap.peek();
 
        // Add it to the sum
        sum += currentMax;
 
        // Remove the current max from the heap
        maxHeap.remove();
 
        // Add the current max back to the
        // heap after decrementing it by 1
        maxHeap.add(currentMax - 1);
    }
    return sum;
}
 
// driver code
public static void main(String args[])
{
    int arr[] = { 2, 3, 5, 4 }, K = 3;
    int n =arr.length;
    System.out.println(getSum(arr, K, n));
}
}
//contributed by Arnab Kundu


Python3
# Python3 implementation of above approach
def getSum(arr, K, n) :
    Sum = 0
    maxHeap = []
    for i in range(n) :
    
        # put all array elements
        # in a max heap
        maxHeap.append(arr[i])
      
    maxHeap.sort()
    maxHeap.reverse()
      
    while (K > 0) :
    
        # Get the current maximum element
        currentMax = maxHeap[0]
    
        # Add it to the sum
        Sum += currentMax
    
        # Remove the current max from the heap
        maxHeap.pop(0)
    
        # Add the current max back to the
        # heap after decrementing it by 1
        maxHeap.append(currentMax - 1)
        maxHeap.sort()
        maxHeap.reverse()
        K -= 1
     
    return Sum
     
arr = [ 2, 3, 5, 4 ]
K = 3;
n = len(arr)
print(getSum(arr, K, n))
 
#. This code is contributed by divyeshrabadiya07.


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG {
  
    static int getSum(int[] arr, int K, int n)
    {
        int sum = 0;
        List maxHeap = new List();
        for (int i = 0; i < n; i++) {
       
            // put all array elements
            // in a max heap
            maxHeap.Add(arr[i]);
        }
         
        maxHeap.Sort();
        maxHeap.Reverse();
         
        while (K-- > 0) {
       
            // Get the current maximum element
            int currentMax = maxHeap[0];
       
            // Add it to the sum
            sum += currentMax;
       
            // Remove the current max from the heap
            maxHeap.RemoveAt(0);
       
            // Add the current max back to the
            // heap after decrementing it by 1
            maxHeap.Add(currentMax - 1);
            maxHeap.Sort();
            maxHeap.Reverse();
        }
        return sum;
    } 
     
  // Driver code
  static void Main()
  {
    int[] arr = { 2, 3, 5, 4 };
    int K = 3;
    int n = arr.Length;
    Console.Write(getSum(arr, K, n));
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
13

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