Budget Shopping Spree
Imagine you're managing procurement for a new project, and you have a strict budget for acquiring necessary components. There's a list of available components, each with a specific cost. Your goal is to acquire as many distinct components as possible without exceeding your allocated budget. This problem tests your ability to optimize resource allocation under constraints, a common scenario in real-world engineering.
Your task is to implement a function that determines the maximum number of distinct components you can purchase given a list of their individual costs and your total budget.
costs: An array of numbers, where costs[i] represents the cost of the i-th component.budget: A number representing the total amount of money you have to spend.costs array is empty, no items can be bought, so the result should be 0.budget is 0, and all items have a cost greater than 0, then 0 items can be bought.costs array is distinct (you can't buy the same 'type' of item multiple times if it only appears once in the array). You are simply selecting items from the given list.Examples
Example 1
Input:
costs = [10, 20, 30], budget = 50
Output:
2
Note:
To maximize items, we buy the cheapest ones first. Buy the item costing 10 (budget remaining: 40, items: 1). Then buy the item costing 20 (budget remaining: 20, items: 2). The next cheapest item costs 30, which exceeds the remaining budget. So, the maximum items bought is 2.
Example 2
Input:
costs = [1, 100, 200], budget = 2
Output:
1
Note:
Buy the item costing 1 (budget remaining: 1, items: 1). The next cheapest item costs 100, which exceeds the remaining budget. Maximum items bought is 1.
Example 3
Input:
costs = [7, 3, 6, 2, 5], budget = 15
Output:
3
Note:
Sort the costs: [2, 3, 5, 6, 7].
Constraints
1 <= costs.length <= 10^51 <= costs[i] <= 10^90 <= budget <= 10^9costs[i] and budget are integers.Loading editor...