📜  使用最小堆最大化可以购买数量为 K 的玩具数量

📅  最后修改于: 2021-10-28 01:53:37             🧑  作者: Mango

给定一个数组arr[]由玩具成本和一个整数K组成,该整数表示可用于购买玩具的金额。任务是找到一个人可以用数量K购买的最大数量的玩具。
注意:一个特定的玩具只能购买 1 个数量。

例子:

方法:将给定数组的所有元素插入到一个 priority_queue 中,现在一个一个地从这个优先级队列中删除元素,并将这些成本添加到初始化为0的变量sum 中。继续删除元素,而新加法保持总和小于K 。最后,删除的元素数将是所需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// maximum toys that can be bought
int maxToys(int arr[], int n, int k)
{
 
    // Create a priority_queue and push
    // all the array elements in it
    priority_queue, greater > pq;
    for (int i = 0; i < n; i++) {
        pq.push(arr[i]);
    }
 
    // To store the count of maximum
    // toys that can be bought
    int count = 0;
    while (pq.top() <= k) {
        count++;
        k = k - pq.top();
        pq.pop();
    }
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 12, 5, 111, 200, 1000, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 50;
 
    cout << maxToys(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to return the count of
// maximum toys that can be bought    
public static int maxToys(int[] arr, int k)
{
    int n = arr.length;
     
    // Create a priority_queue and push
    // all the array elements in it
    PriorityQueue pq = new PriorityQueue();
    for(int i = 0; i < n; i++)
    {
        pq.offer(arr[i]);
    }
     
    // To store the count of maximum
    // toys that can be bought
    int count = 0;
    while (!pq.isEmpty() && pq.peek() <= k)
    {
        k = k - pq.poll();
        count++;
    }
    return count;
}
 
// Driver code
public static void main (String[] args)
{
    int[] arr = new int[]{ 1, 12, 5, 111,
                           200, 1000, 10 };
    int k = 50;                      
     
      System.out.println(maxToys(arr, k));     
}
}
 
// This code is contributed by ankit bajpai


Python3
# Python3 implementation of the approach
 
# Function to return the count of
# maximum toys that can be bought
def maxToys(arr, n, k) :
   
    # Create a priority_queue and push
    # all the array elements in it
    pq = []
    for i in range(n) :
        pq.append(arr[i])
    pq.sort()
   
    # To store the count of maximum
    # toys that can be bought
    count = 0
    while (pq[0] <= k) :
        count += 1
        k = k - pq[0]
        pq.pop(0)
    return count
     
    # Driver code
arr = [ 1, 12, 5, 111, 200, 1000, 10 ]
n = len(arr)
k = 50
print(maxToys(arr, n, k))
 
# This code is contributed by divyesh072019


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to return the count of
    // maximum toys that can be bought
    static int maxToys(int[] arr, int n, int k)
    {
      
        // Create a priority_queue and push
        // all the array elements in it
        List pq = new List();
        for (int i = 0; i < n; i++)
        {
            pq.Add(arr[i]);
        }
         
        pq.Sort();
      
        // To store the count of maximum
        // toys that can be bought
        int count = 0;
        while (pq[0] <= k)
        {
            count++;
            k = k - pq[0];
            pq.RemoveAt(0);
        }
        return count;
    }
 
  // Driver code
  static void Main()
  {
    int[] arr = { 1, 12, 5, 111, 200, 1000, 10 };
    int n = arr.Length;
    int k = 50;
    Console.WriteLine(maxToys(arr, n, k));
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
4

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

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