Problem · Greedy
Planning Production
Learn this problemProblem 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[]) → longExamples
Example 1
worstCase = [6, 5, 7]expected = [4, 2, 1]return = 9The optimal production order is 2, 1, 0:
- Start with
9units of cash. - Produce product
2: requires7units worst-case, spends1unit expected.- Remaining cash:
9 - 1 = 8units
- Remaining cash:
- Produce product
1: requires5units worst-case, spends2units expected.- Remaining cash:
8 - 2 = 6units
- Remaining cash:
- Produce product
0: requires6units worst-case, spends4units expected.- Remaining cash:
6 - 4 = 2units
- Remaining cash:
Therefore, the minimum starting amount is 9 units of cash.