AMAZON Coding Question – Solved

6 Live
Drone Delivery Optimization – Amazon's Challenge In Amazon's vast distribution network, drones are essential for delivering packages. These drones have varying capacities, ranging from 1 to 10⁹. Each j-th drone has a carrying capacity of j. Problem Statement Amazon needs to dispatch n packages, where the weight of the i-th package is given by the array pack[i].During peak delivery times, only two drones are available for transport. These drones must alternate in their duties. This means: - If Drone 1 delivers the i-th package, - Then Drone 2 must deliver the (i+1)-th package, and so on. However, not all drones can carry all packages — some packages might be too heavy for a particular drone. To address this issue, Amazon can replace certain packages with others of a different (lighter) weight to ensure successful delivery. Task Your task is to determine the minimum number of package replacements needed, such that all packages can be successfully delivered by some pair of drones, alternating turns. You are allowed to choose any two drones with any capacity. Example n = 4 pack = [3, 1, 3, 2]

Asked in: AMAZON

Image of the Question

Question Image

All Testcases Passed ✔



Passcode Image

Solution


#!/bin/python3

import math
import os
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To tackle the drone delivery optimization problem, you need to understand and balance several key constraints and objectives. The problem involves assigning two drones, each with a specific carrying capacity, to deliver a sequence of packages in an alternating manner while minimizing how many packages you need to replace with lighter alternatives so that every package can be carried by its assigned drone.

Step 1: Understand the setup
- There are n packages with weights given by an array.
- Two drones must deliver the packages, taking turns alternately. If drone A carries package 1, drone B must carry package 2, drone A again for package 3, and so forth.
- Each drone has a capacity representing the maximum weight it can carry, and you can pick any two drones with capacities from 1 to 10^9.
- You want to minimize the number of replacements (lightening packages) so that all deliveries are possible.

Step 2: Key constraints and variables
- Drones alternate deliveries strictly. This creates two sequences: the packages delivered by drone 1 (all packages at even or odd positions) and those delivered by drone 2 (the others).
- Each drone’s capacity must be at least the heaviest package assigned to it, or else that package needs to be replaced (counted as one replacement).
- Since you want to minimize replacements, ideally, the drone capacity should be set at least as high as the maximum package weight it delivers. However, if you choose drone capacities too small, more packages need replacement. If you choose capacities too large, it might seem optimal, but capacity choices affect only the replacement count, not an absolute cost, so you want to balance these.

Step 3: Break down the problem
Separate packages into two groups based on their position in the delivery sequence:
- Group 1: Packages at indices 0, 2, 4, ... delivered by drone A.
- Group 2: Packages at indices 1, 3, 5, ... delivered by drone B.

For each group:
- Find the distribution of package weights.
- You want to find a drone capacity that minimizes the count of packages heavier than that capacity (which need replacement).

Step 4: Insight about capacity selection
- Drone capacity directly affects the number of replacements: packages heavier than capacity must be replaced.
- To minimize replacements, choose the capacity to be one of the package weights in that group because setting the capacity to a value between two package weights will not reduce replacements compared to setting it exactly to a package weight.
- By iterating through possible capacity candidates (derived from package weights in each group), you can calculate the number of replacements needed if you pick that capacity.

Step 5: Combining both drones’ capacities
You have two groups and you want to pick a capacity for drone A and drone B, so that when alternating deliveries happen, total replacements (sum of replacements for group A and group B) is minimized.
- For every candidate capacity for drone A (from group A’s weights), and for every candidate capacity for drone B (from group B’s weights), calculate the total replacements.
- Keep track of the minimum sum replacements over all pairs of capacity choices.

Step 6: Optimize the approach
- Since packages can be large in number, a brute-force check of all pairs might be too expensive.
- Use sorting and prefix sums or binary search to efficiently calculate replacements for each capacity candidate. For example, sorting each group allows you to quickly find how many packages exceed a certain capacity.
- Precompute cumulative counts so that for any candidate capacity you can get the number of packages exceeding that capacity in O(log n) or O(1) time.

Step 7: Output the minimal number of replacements
- After evaluating all pairs of capacities efficiently, you will have the minimal total replacements needed to ensure that two drones with those capacities can alternate delivery without failing due to weight constraints.

Step 8: Edge cases and validation
- Consider the case where no replacements are needed (both drones have capacities at least as large as the heaviest packages in their respective groups).
- Consider cases where all packages are very heavy or very light.
- Validate the alternating pattern enforcement and that capacity selections are always valid drone capacities (in range 1 to 10^9).

Summary:
The approach revolves around dividing the packages into two groups based on delivery order, then choosing drone capacities smartly for each group from package weights in that group to minimize replacements. Efficient counting techniques allow for rapid calculation of replacements for candidate capacities. The result is the minimal number of package replacements needed for a feasible alternating drone delivery plan.
```


Related Questions