Problem · Greedy

Max Batches For Shipment

MediumAmazonOA
See Amazon hiring insights

There are n unique categories of goods. You are given an array inventory of length n, where inventory[i] denotes the count of items available for category i (with i ranging from 0 to n - 1). These goods need to be organized into groups for delivery.

Group Packing Conditions:

  • No two items within a single group can belong to the same category — every item in a group must be of a distinct category.
  • The count of items placed in the current group must be strictly greater than the count in the previous group.
  • Each item can be used only once, and it is not mandatory to pack all items.
  • Goal:

  • Calculate the maximum number of groups that can be created for delivery based on the above conditions.
  • Examples
    01 · Example 1
    inventory = [2, 3, 1, 4, 2]
    return = 4
    The initial bundle holds 1 unit from type 4. Leftover stock: [2, 3, 1, 4, 1] The second bundle packs 2 units from types 0 and 1. Remaining stock: [1, 2, 1, 4, 1] The third bundle consists of 3 units from types 0, 1, and 3. Remaining stock: [0, 1, 3, 1] The fourth bundle carries 4 units from types 1, 2, 3, and 4. Remaining stock: [0, 0, 0, 2, 0] So we get 4 as our expected output :)
    More Amazon problems
    drafts saved locally
    public int maxBatchesForShipment(int inventory[]) {
      // write your code here
    }
    
    inventory[2, 3, 1, 4, 2]
    expected4
    checking account