Amazon New Grad OA
Anonymous User
701

Finished Amazon New Grad OA today but couldn't solve one of the coding problems.

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.

For example, say we have 6->6->6->3->4 (linked back to the first 6), we can optimally start from the first 6 and collect 1 item, repeat the same for the following 2 warehouses and deliver 2 to 3 and 1 to 4. 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.

I figured this was similar to LC 134 Gas Station but could not come up with a solution without using brute force.

Any ideas? Appreciate the help in advance!

Comments (4)