Problem · Array

Maximum Purchases with a Cooler-Bag Fee

Learn this problem
Mediuminfosys logoinfosysNEW GRADOA

Problem 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) → int

Examples

Example 1

costs = [4,2,7,1]types = [0,1,1,0]budget = 10coolerCost = 3return = 3

Items 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 = 2

The two type-0 items fit exactly, while any type-1 purchase requires more than the budget.

Constraints

  • 1 <= costs.length == types.length <= 200000
  • 1 <= costs[i] <= 1000000000
  • types[i] is 0 or 1.
  • 0 <= budget <= 10^18
  • 0 <= coolerCost <= 1000000000

More infosys problems

drafts saved locally
public int maxPurchasableItems(int[] costs, int[] types, long budget, int coolerCost) {
    // Write your code here.
}
costs[4,2,7,1]
types[0,1,1,0]
budget10
coolerCost3
expected3
checking account