Problem · Greedy

Planning Production

Learn this problem
MediumVisaOA

Problem statement

You must plan production for a company that manufactures multiple products. For each product i:

  • worstCase[i] is the minimum cash that must be available before production begins.
  • expected[i] is the cost actually spent during production.

Determine the minimum amount of starting cash needed to manufacture all products. Products can be produced in any order, and after completing each product, the remaining cash can be used for subsequent products.

Function

plenProduction(worstCase: int[], expected: int[]) → long

Examples

Example 1

worstCase = [6, 5, 7]expected = [4, 2, 1]return = 9

The optimal production order is 2, 1, 0:

  1. Start with 9 units of cash.
  2. Produce product 2: requires 7 units worst-case, spends 1 unit expected.
    • Remaining cash: 9 - 1 = 8 units
  3. Produce product 1: requires 5 units worst-case, spends 2 units expected.
    • Remaining cash: 8 - 2 = 6 units
  4. Produce product 0: requires 6 units worst-case, spends 4 units expected.
    • Remaining cash: 6 - 4 = 2 units

Therefore, the minimum starting amount is 9 units of cash.

More Visa problems

drafts saved locally
public long plenProduction(int[] worstCase, int[] expected) {
  // write your code here
}
worstCase[6, 5, 7]
expected[4, 2, 1]
expected9
checking account