Problem · Array
Maximum Purchases with a Cooler-Bag Fee
Learn this problemProblem statement
Each item has a positive cost in costs and a type in types: 0 or 1. You may buy each item at most once without exceeding budget.
If you buy at least one type-1 item, you must also pay coolerCost exactly once. Buying only type-0 items does not require that fee.
Return the maximum number of items you can buy.
Function
maxPurchasableItems(costs: int[], types: int[], budget: long, coolerCost: int) → intExamples
Example 1
costs = [4,2,7,1]types = [0,1,1,0]budget = 10coolerCost = 3return = 3Items costing 1, 2, and 4 plus the one-time fee cost exactly 10.
Example 2
costs = [2,3,4]types = [0,0,1]budget = 5coolerCost = 5return = 2The two type-0 items fit exactly, while any type-1 purchase requires more than the budget.
Constraints
1 <= costs.length == types.length <= 2000001 <= costs[i] <= 1000000000types[i]is 0 or 1.0 <= budget <= 10^180 <= coolerCost <= 1000000000