FastPrepFastPrep
Problem Brief

Minimize Warehouse Transfer Cost

NEW GRADOA
See Amazon online assessment and hiring insights

Note πŸ“ - Initially, I thought this problem might be a duplicate of an existing one, but I wasn't able to find it. If you happen to come across it as a duplicate, please let me know! Thank you so much in advance!! You are the best!! 🐿️

Basically, Amazon has its warehouses lined up in a circle, you can start from any warehouse move in either clockwise or anti-clockwise direction, the direction must remain the same throughout the remaining moves.

Each warehouse stores some items. The goal is to collect excess items from some warehouses and deliver them to others need them, so that each warehouse stores the same number of items in the end (guaranteed).

The distance between 2 adjacent warehouses is 1, and the cost of each product transfer is the distance the product is moved.

Function Description

Complete the function minimizeWarehouseTransferCost in the editor.

minimizeWarehouseTransferCost has the following parameter:

  1. int[] warehouses: an array of integers representing the number of items in each warehouse

Returns

long integer: the minimum cost to make all warehouses store the same number of items

1Example 1

Input
warehouses = [6, 6, 6, 3, 4]
Output
7
Explanation
Note πŸ“ - Just as the explanation said, 7 is just a possible solution but not the optimal one the problem asks for. I'd leave it as it is for now and will come back to update it once I find more reliable references. Or if you happen to know about the optimal output, plzzz let me know! Thank you so much!! We can optimally start from the first warehouse and collect 1 item, repeat the same for the following 2 warehouses and deliver 2 to the 4th warehouse and 1 to the 5th warehouse. In the end, each warehouse has 5 items and the total cost is 1 + 2 + 3 + 1 = 7. This is just a possible clockwise move, the optimal cost should consider the anti-clockwise direction as well and return the minimum cost.

Constraints

Limits and guarantees your solution can rely on.

πŸ₯πŸ₯
public long minimizeWarehouseTransferCost(int[] warehouses) {
  // write your code here
}
Input

warehouses

[6, 6, 6, 3, 4]

Output

7

Sign in to submit your solution.