📌  相关文章
📜  最大化可购买数量为K的玩具的数量

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

给定一个由玩具成本组成的数组。给定一个整数K来描述可用于购买玩具的金额。编写一个程序,以找到一个数量为K的玩具可以购买的最大数量。

注意:只能购买1数量的特定玩具。

例子 :

Input:  N = 10, K =  50
        cost = { 1, 12, 5, 111, 200, 1000, 10, 9, 12, 15 }
Output: 6
Explanation: Toys with amount 1, 5, 9, 10, 12, and 12 
can be purchased resulting in a total amount of 49. Hence,
maximum number of toys is 6.

Input: N = 7, K = 50
       cost = { 1, 12, 5, 111, 200, 1000, 10 }
Output: 4

解决此问题的想法是首先按升序对成本数组进行排序。这将以增加成本的顺序排列玩具。现在遍历成本数组,并继续计算成本总和,直到总和小于或等于K。最后返回用于计算总和的玩具数量,该数量刚好小于或等于K。

下图说明了上述方法:

下面是上述方法的实现:

C++
// C++ Program to maximize the
// number of toys with K amount
#include 
using namespace std;
  
// This functions returns the required
// number of toys
int maximum_toys(int cost[], int N, int K)
{
    int count = 0, sum = 0;
  
    // sort the cost array
    sort(cost, cost + N);
    for (int i = 0; i < N; i++) {
  
        // Check if we can buy ith toy or not 
        if (sum +cost[i] <= K) 
        {
            sum = sum + cost[i];
            // Increment count
            count++;
        }
    }
    return count;
}
  
// Driver Code
int main()
{
    int K = 50;
    int cost[] = { 1, 12, 5, 111, 200, 1000, 10, 9, 12, 15 };
    int N = sizeof(cost) / sizeof(cost[0]);
  
    cout << maximum_toys(cost, N, K) << endl;
    return 0;
}


Java
// Java Program to maximize the
// number of toys with K amount
import java.io.*;
import java .util.*;
  
class GFG 
{
// This functions returns 
// the required number of toys
static int maximum_toys(int cost[], 
                        int N, int K)
{
    int count = 0, sum = 0;
  
    // sort the cost array
    Arrays.sort(cost);
    for (int i = 0; i < N; i++) 
    {
  
        // Check if we can buy ith toy or not 
        if (sum +cost[i] <= K) 
        {
            sum = sum + cost[i];
            // Increment count
            count++;
        }
    }
    return count;
}
  
// Driver Code
public static void main (String[] args) 
{
int K = 50;
int cost[] = {1, 12, 5, 111, 200,
            1000, 10, 9, 12, 15};
int N = cost.length;
  
System.out.print( maximum_toys(cost, N, K));
}
}
  
// This code is contributed by anuj_67.


Python3
# Python 3 Program to maximize the
# number of toys with K amount
  
# This functions returns the required
# number of toys
def maximum_toys(cost, N, K):
    count = 0
    sum = 0
  
    # sort the cost array
    cost.sort(reverse = False)
    for i in range(0, N, 1):
          
        # Check if we can buy ith toy or not
        if (sum+cost[i] <= K):
            sum = sum + cost[i]
            # Increment the count variable
            count += 1
      
    return count
  
# Driver Code
if __name__ == '__main__':
    K = 50
    cost = [1, 12, 5, 111, 200, 
            1000, 10, 9, 12, 15]
    N = len(cost)
  
    print(maximum_toys(cost, N, K))
  
# This code is contributed by
# Sanjit_Prasad


C#
// C# Program to maximize the
// number of toys with K amount
using System;
  
class GFG 
{
// This functions returns 
// the required number of toys
static int maximum_toys(int []cost, 
                        int N, int K)
{
    int count = 0, sum = 0;
  
    // sort the cost array
    Array.Sort(cost);
    for (int i = 0; i < N; i++) 
    {
  
        // Check if we can buy ith toy or not 
        if (sum +cost[i] <= K) 
        {
            sum = sum + cost[i];
            // Increment count
            count++;
        }
    }
    return count;
}
  
// Driver Code
public static void Main () 
{
int K = 50;
int []cost = {1, 12, 5, 111, 200,
            1000, 10, 9, 12, 15};
int N = cost.Length;
  
Console.Write( maximum_toys(cost, N, K));
}
}
  
// This code is contributed by anuj_67.


PHP


输出 :
6

时间复杂度: O(N * logN),其中N是成本数组的大小。