Problem: Get Minimum Connection Cost
In 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
(assuming 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 i
to a hub at position j
is given by warehouseCapacity[j] - warehouseCapacity[i]
.
Given a series of queries of the form (hubA, hubB)
, where two additional high-performance distribution hubs are deployed at warehouses hubA
and hubB
(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.
Notes:
- The problem statement assumes 1-based indexing for the
warehouseCapacity
array. - Each query is independent, i.e., the changes made do not persist for subsequent queries.
- Each warehouse connects to the nearest hub at or beyond its position (either
hubA
,hubB
, or the central hub atn
) to minimize the overall connection cost.
Function Description
Complete the function getMinConnectionCost
as described below.
Function Parameters:
int warehouseCapacity[n]
: A non-decreasing array of integers representing the storage capacities of the warehouses.int additionalHubs[q][2]
: A 2D array where each element denotes the positions of two additional distribution hubs installed for a query.
Returns:
long int[q]
: The minimum total connection costs for each query.
Examples
Example 1:
Input:warehouseCapacity = [3, 6, 10, 15, 20]
,q = 1
,additionalHubs = [[2, 4]]
Output:[8]
Explanation:
- Additional hubs are installed at positions
2
and4
. - For each warehouse:
- Warehouse
1
connects to hub2
with cost6 - 3 = 3
. - Warehouse
2
is itself a hub, so cost is0
. - Warehouse
3
connects to hub4
with cost15 - 10 = 5
. - Warehouses
4
and5
are themselves hubs, so costs are0
.
- Warehouse
Total cost: 3+0+5+0+0=83 + 0 + 5 + 0 + 0 = 8.
Example 2:
Input:warehouseCapacity = [0, 2, 5, 9, 12, 18]
,q = 2
,additionalHubs = [[2, 5], [1, 3]]
Output:[12, 18]
Explanation:
Query 1: Hubs at positions 2
and 5
:
- Warehouse
1
connects to hub2
, cost2 - 0 = 2
. - Warehouse
2
is itself a hub, cost0
. - Warehouse
3
connects to hub5
, cost12 - 5 = 7
. - Warehouse
4
connects to hub5
, cost12 - 9 = 3
. - Warehouses
5
and6
are hubs, costs0
.
Total cost: 2+0+7+3+0+0=122 + 0 + 7 + 3 + 0 + 0 = 12.
Query 2: Hubs at positions 1
and 3
:
- Warehouse
1
is a hub, cost0
. - Warehouse
2
connects to hub3
, cost5 - 2 = 3
. - Warehouse
3
is a hub, cost0
. - Warehouse
4
connects to hub6
, cost18 - 9 = 9
. - Warehouse
5
connects to hub6
, cost18 - 12 = 6
. - Warehouse
6
is a hub, cost0
.
Total cost: 0+3+0+9+6+0=180 + 3 + 0 + 9 + 6 + 0 = 18.
Constraints:
1 <= n <= 10^5
1 <= q <= 100
1 <= warehouseCapacity[i] <= 10^9
warehouseCapacity
is sorted in non-decreasing order.1 <= hubA < hubB < n
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.