Compute Minimum Total Distance
Learn this problemProblem statement
FastPrep.io has recently expanded its operations by establishing n distribution centers in a newly developed location in Cal. To efficiently manage the supply chain, they plan to set up two warehouses that will serve these distribution centers.
It is important to note that both the distribution centers and warehouses are positioned along a straight line. Each distribution center will have its demands fulfilled by the nearest warehouse.
A dedicated logistics team is tasked with determining the optimal locations for these two warehouses. Their primary objective is to minimize the total sum of distances between each distribution center and its closest warehouse, ensuring the most efficient and cost-effective supply chain operations.
Please complete function computeMinimumTotalDistance in the editor. This function has a parameter called distribution_center_locations, which is an integer array representing the positions of the distribution centers along the straight line. The function should return the minimum total distance between the distribution centers and the warehouses closest to them.
🦉Supa grateful for having a reliable helper like 🌷spike🌷 around 😊
Function
computeMinimumTotalDistance(distribution_center_locations: int[]) → intExamples
Example 1
distribution_center_locations = [1, 2, 3]return = 1
Example 2
distribution_center_locations = [1, 6]return = 0Example 3
distribution_center_locations = [1, 2, 5, 6]return = 2Constraints
2 <= n <= 2 * 1030 <= distribution_center_locations[i] <= 106
More Amazon problems
- Drone Delivery RouteOA · Seen Jul 2026
- Detect a Keyword SubstringONSITE INTERVIEW · Seen Jul 2026
- Find Maximum Total Amount (SDE I, Fungible :)OA · Seen Jul 2026
- Find the Root of a Directed TreeONSITE INTERVIEW · Seen Jul 2026
- Meeting Rooms IIPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · ONSITE INTERVIEW · Seen Jul 2026
- Single Element in a Sorted ArrayPHONE SCREEN · Seen Jul 2026
- Count the Number of Complete ComponentsPHONE SCREEN · Seen Jul 2026