KatInterview

Minimum Transfers to Rebalance a Four-Zone Warehouse

Greedy & IntervalsAuto-graded · 22 test cases
MediumAuto-gradedGreedy & Intervalsgreedyarraysoptimization

Last updated

Tests whether you can compute the minimum number of item relocations to rebalance inventory across four warehouse zones using surplus/deficit reasoning.

Minimum Transfers to Rebalance a Four-Zone Warehouse

A warehouse is divided into exactly four zones, numbered 1 through 4. Each zone holds some number of inventory units. You are given the current unit counts across the four zones and a target distribution you need to reach.

In a single transfer, you pick one unit from any zone and move it to any other zone. Units are interchangeable — only the count per zone matters, not which specific unit goes where.

Write a function that computes the minimum number of transfers needed to reach the target distribution, and returns one valid sequence of transfers that achieves it.

A transfer is represented as a two-element list [from_zone, to_zone], where both values are zone numbers (1–4).

Output format

Return a dictionary with exactly two keys:

  • "min_moves": an integer, the minimum number of transfers required.
  • "moves": a list of [from_zone, to_zone] pairs, one per transfer, in the order they are applied.

When multiple optimal sequences exist, output the canonical one: build the sequence by pairing zones with excess units against zones with missing units, processing both groups in ascending zone order.

Precondition

The total number of units is the same in both current and target. You may assume this holds for all inputs.

Constraints

  • Both current and target are lists of exactly 4 non-negative integers.
  • sum(current) == sum(target) for all valid inputs.
  • Each element of current and target satisfies 0 ≤ value ≤ 10^5.
  • If the distribution is already correct, return {"min_moves": 0, "moves": []}.

Examples

Example 1

Input: rebalance_warehouse([3, 2, 1, 0], [1, 2, 2, 1])

Output: {'min_moves': 2, 'moves': [[1, 3], [1, 4]]}

Example 2

Input: rebalance_warehouse([1, 1, 1, 1], [1, 1, 1, 1])

Output: {'min_moves': 0, 'moves': []}

Example 3

Input: rebalance_warehouse([0, 5, 0, 0], [2, 0, 1, 2])

Output: {'min_moves': 5, 'moves': [[2, 1], [2, 1], [2, 3], [2, 4], [2, 4]]}

Example 4

Input: rebalance_warehouse([4, 0, 3, 1], [2, 3, 1, 2])

Output: {'min_moves': 4, 'moves': [[1, 2], [1, 2], [3, 2], [3, 4]]}

Hints

Hint 1

For each zone, compute how many units it has in excess or in deficit relative to the target. What does the sum of all excesses tell you?

Hint 2

The minimum number of transfers equals the total excess across all over-stocked zones (which equals the total deficit across all under-stocked zones). Think about why no single transfer can do better than reduce this total by 1.

Hint 3

To build the move sequence, expand each over-stocked zone into a list of individual source slots and each under-stocked zone into a list of destination slots, then pair them up in zone order.