AMAZON Coding Question – Solved

9 Live
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

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


import math
def getMaximumCredits(inventory, dispatch1, dispatch2, skips):
    arr = []
    val = dispatch1+dispatch2
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem efficiently, begin by understanding the process flow of inventory dispatch from each warehouse. The dispatch cycle is repeated as follows: you reduce the inventory first by a fixed amount (dispatch1), followed by your co-worker reducing it by another fixed amount (dispatch2). The goal is to earn a credit if the inventory is emptied during your turn, i.e., the warehouse’s stock becomes zero or negative directly after your dispatch.

The challenge arises from the co-worker’s ability to skip their turn up to a limited number of times (skips). Skipping allows you to take consecutive turns, thereby increasing the likelihood of you being the one to empty a warehouse and earn the credit for that warehouse. However, because the number of skips is limited, a strategy must be devised to apply skips where they have the most value — specifically, where they convert a non-credit (your co-worker would have emptied the warehouse) into a credit for you.

To devise a solution strategy, think in terms of simulating the dispatch cycle for each warehouse and analyzing the number of turns it takes to drain its inventory to zero or less. For each warehouse, simulate the process under two modes:
1. When no skip is used — i.e., your co-worker takes their turn every time.
2. When a skip is used — i.e., your co-worker skips once, allowing you to take two consecutive turns.

Start by computing the number of full dispatch cycles (your turn followed by co-worker’s) needed to deplete each warehouse’s inventory. At each step, subtract dispatch1, then subtract dispatch2, and repeat until the inventory reaches or drops below zero. The point at which the inventory is depleted tells you who emptied the warehouse — either you (earning a credit) or your co-worker (no credit).

Next, simulate an alternative where your co-worker skips one turn early in the process, giving you two consecutive dispatches. Recalculate the sequence with this skip applied and determine whether the skip changes the outcome — does it convert a warehouse where your co-worker would’ve gotten the credit into one where you get the credit?

With this analysis, you can classify the warehouses into:
- Type A: You would already get a credit even without any skips.
- Type B: You would not get a credit unless your co-worker skips.
- Type C: Even with a skip, you still don’t get the credit.

Type A warehouses always contribute to the credit count and require no skips. For Type B warehouses, you can potentially earn an additional credit if you apply a skip. Therefore, your goal becomes to identify all Type B warehouses, compute how many of them exist, and then decide where to apply your limited skips for maximum gain.

Sort the Type B warehouses by the potential benefit (they all provide a +1 credit if a skip is applied), and select up to 'skips' number of these warehouses to apply your co-worker’s skips. You only want to apply skips to those where it actually increases the total number of credits. Skipping in Type A or C warehouses is wasteful.

The final credit total becomes:
- Credits from all Type A warehouses (automatically earned without any skips).
- Plus the number of Type B warehouses to which you applied skips (up to the limit of the 'skips' value).

Given the constraints (up to 1e5 warehouses and large inventory values), it is crucial to avoid full simulations for each warehouse. Instead, derive a mathematical formula or expression to determine who would have emptied the warehouse in how many turns. Since dispatch amounts are fixed, the process can be predicted with arithmetic rather than iteration.

For instance, you can compute the number of your own dispatches before the warehouse is emptied and compare it to the total number of turns. Determine the number of turns it takes to deplete the inventory by calculating how many (dispatch1 + dispatch2) cycles are required, and where the inventory stands after each. Then, consider what happens if a skip is inserted: does it reduce the number of cycles needed, or shift the sequence so that you now perform the final dispatch?

By analyzing each warehouse in this deterministic way, you can classify the outcomes efficiently and maximize your credits under the given constraints. This approach ensures that the problem can be solved in linear time with respect to the number of warehouses, making it feasible within large input bounds.
```


Related Questions