Compute Lowest Linking (Connection) Expense
Learn this problemProblem statement
Within Amazon's highly efficient logistics network, minimizing operational overhead and optimizing package routing is crucial to ensure smooth deliveries across various regions.
The network consists of n warehouses, numbered from 1 to n, each strategically positioned at its corresponding index. Each warehouse has a specific storage capacity, given by warehouseCapacity, where warehouseCapacity[i] represents the capacity of the warehouse located at position i (using 1-based indexing).
These warehouses are organized in a non-decreasing order of their storage capacities, meaning each warehouse's storage capacity is greater than or equal to the one before it. Each warehouse must establish a connection to a distribution hub positioned at a location greater than or equal to its own. This means that a warehouse at position i can only connect to a hub at position j, where j >= i.
To optimize inventory routing, Amazon has placed a central high-capacity distribution hub at the last warehouse, located at position n. This hub serves as the main connection point for all warehouses if necessary. The cost of establishing a connection from warehouse at i to a hub at position j is given by warehouseCapacity[j] - warehouseCapacity[i].
Given q queries of the form (hubA, hubB), where two additional high-performance distribution hubs are deployed at warehouses hubA and hubB (with 1 <= hubA < hubB <= n), the goal is to calculate the minimum total connection cost for all warehouses, considering the nearest available distribution hub at or beyond each warehouse's position.
Each warehouse connects to the nearest hub at or beyond its position (either hubA, hubB, or the central hub at n) to minimize the overall connection cost.
Each query is independent; the additional hubs deployed for one query do not persist for subsequent queries.
Function
getMinConnectionCost(storageLimit: int[], q: int, extraHubs: int[][]) → long[]Complete the function getMinConnectionCost with the following parameters:
int warehouseCapacity[n]: a non-decreasing array of integers representing the storage capacities of the warehouses.int additionalHubs[q][2]: an array where each element is a pair of integers denoting the positions of the two additional distribution hubs for a query.
Returns:
long int[q]: the minimum total connection cost for each query.
Examples
Example 1
storageLimit = [3, 6, 10, 15, 20]q = 1extraHubs = [[2, 4]]return = [8]Example 2
storageLimit = [0, 2, 5, 9, 12, 18]q = 2extraHubs = [[2, 5], [1, 3]]return = [12, 18]Example 3
storageLimit = [2, 6, 8, 14]q = 1extraHubs = [[1, 2]]return = [6]Constraints
warehouseCapacityis given in non-decreasing order (each element is greater than or equal to the previous).- The array uses 1-based indexing.
- For each query, the two additional hub positions satisfy
1 <= hubA < hubB <= n. - A warehouse at position
imay only connect to a hub at positionjwherej >= i. - A central distribution hub is always present at position
n. - Each query is independent; deployments do not persist across queries.
- The connection cost for connecting warehouse
ito hubjiswarehouseCapacity[j] - warehouseCapacity[i]. - The result for each query may exceed 32-bit integer range and must be returned as a
long.
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026