FastPrepMaximum Alloy Production Within Budget

Maximum Alloy Production Within Budget

Microsoft logoMicrosoft● MediumINTERNOA
Learn

Problem statement

A foundry produces one alloy using n different metals. For each metal i:

  • composition[i] is the quantity required to produce one unit of alloy.
  • stock[i] is the quantity already available.
  • cost[i] is the purchase price per additional unit.

You may buy any nonnegative integer quantity of each metal, spending at most budget in total. Return the maximum whole number of alloy units that can be produced using the initial stock plus the purchased metals.

Function

maxAlloyUnits(composition: int[], stock: int[], cost: int[], budget: long) → long

Examples

Example 1

composition = [1,2]stock = [0,1]cost = [1,1]budget = 3return = 1

One unit needs purchases [1,1] costing 2. Two units need purchases [2,3] costing 5, which exceeds the budget.

Example 2

composition = [2,1]stock = [4,0]cost = [3,2]budget = 4return = 2

The existing first metal covers two alloy units, and buying two units of the second metal costs exactly 4.

Example 3

composition = [3]stock = [10]cost = [5]budget = 0return = 3

The stock alone produces three complete units, with one unit of metal left over.

Constraints

  • 1 <= composition.length == stock.length == cost.length <= 100000.
  • 1 <= composition[i] <= 10^9.
  • 0 <= stock[i] <= 10^9.
  • 1 <= cost[i] <= 10^9.
  • 0 <= budget <= 10^18.
  • The answer fits in a signed 64-bit integer.

More Microsoft problems

See Microsoft hiring insights
public long maxAlloyUnits(int[] composition, int[] stock, int[] cost, long budget) {
    // Write your code here.
}
composition[1,2]
stock[0,1]
cost[1,1]
budget3
expected1
Checking account…