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

📅  最后修改于: 2021-04-17 12:19:58             🧑  作者: Mango

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

例子:

方法:现在将一个给定数组的所有元素插入一个priority_queue中,从此优先级队列中一个一个地删除元素,并将这些成本添加到初始化为0的变量总和中。继续删除元素,而新添加的元素使总和小于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