AMAZON Coding Question β Solved
Amazon operates numerous warehouses, with each warehouse holding inventory[i] units of a particular product. You and your co-worker are responsible for dispatching these items to fulfill customer orders, following a specific process:
1. When dispatching from warehouse i, you begin by reducing the inventory of the i-th warehouse by dispatch1 units.
2. After your dispatch, your co-worker reduces the inventory by dispatch2 units.
3. This process repeats until the inventory of the i-th warehouse reaches zero or becomes negative (i.e., inventory[i] β€ 0).
4. For every warehouse that is emptied during your dispatch, you and your co-worker collectively earn 1 credit.
Your co-worker has the option to skip their turn, but they can only do this a limited number of times, defined by `skips`. Your task is to determine the best strategy to maximize the total credits that both you and your co-worker can earn together.
Example:
n = 6
inventory = [10, 6, 12, 8, 15, 1]
dispatch1 = 2
dispatch2 = 3
skips = 3
An optimal dispatch strategy is as follows:
1. Your co-worker skips 2 turns, allowing you to empty the inventory of the 1st warehouse (Inventory: 10 β 8 β 5 β 3 β 1 β -1).
2. Your co-worker doesn't skip any turns, and you empty the inventory of the 2nd warehouse (Inventory: 6 β 4 β 1 β -1).
3. Your co-worker doesn't skip any turns, and you empty the inventory of the 3rd warehouse (Inventory: 12 β 10 β 7 β 5 β 2 β 0).
4. Your co-worker skips 1 turn, and you drain the inventory of the 4th warehouse (Inventory: 8 β 6 β 3 β 1 β -1).
5. Your co-worker doesn't skip any turns, and they empty the inventory of the 5th warehouse (Inventory: 15 β 13 β 10 β 8 β 5 β 3 β 0).
6. Your co-worker doesn't skip any turns, and you empty the inventory of the 6th warehouse (Inventory: 1 β -1).
As a result, the 1st, 2nd, 3rd, 4th, and 6th warehouses were completely dispatched by you, and the two of you collectively earned 5 credits, which is the maximum possible in this scenario. Hence, the answer is 5.
Function Description:
Complete the function `getMaximumCredits` in the editor below.
`getMaximumCredits` has the following parameters:
- int inventory[n]: An array of integers denoting the inventory level of each warehouse.
- int dispatch1: An integer indicating your dispatch level per turn.
- int dispatch2: An integer indicating your co-worker's dispatch level per turn.
- int skips: An integer specifying the maximum number of times your co-worker can skip their turn.
Return:
- int: the maximum number of credits both of you can achieve collectively.
Constraints:
- 1 β€ n β€ 10^5
- 1 β€ inventory[i] β€ 10^9
- 1 β€ dispatch1, dispatch2 β€ 10^9
- 0 β€ skips β€ n
Asked in:
AMAZON