KatInterview

Build Three-Color Outfits from Limited Inventory

Heaps, Sorting & SearchAuto-graded · 19 test cases
MediumAuto-gradedHeaps, Sorting & Searchgreedyheapcombinatorics

Last updated

Tests greedy allocation with heap-based selection and mathematical reasoning about resource constraints under distinct-element requirements.

You manage an inventory of clothing items, each tagged with a color represented by an integer index. Your goal is to assemble outfits where each outfit consists of exactly 3 items, and all three items must have different colors. Each item can be used in at most one outfit.

Given an array inventory where inventory[i] represents the number of items available in color i, determine:

  1. The maximum number of complete outfits you can create
  2. A valid assignment showing which colors to use for the first few outfits (up to 10)

Return a list containing two elements:

  • The first element is an integer: the maximum number of outfits possible
  • The second element is a list of lists: each inner list contains 3 distinct color indices representing one outfit. Include assignments for the first min(10, max_outfits) outfits.

Note: Multiple valid assignments may exist; any correct assignment is acceptable.

Constraints

  • 0 ≤ len(inventory) ≤ 1000
  • 0 ≤ inventory[i] ≤ 10^6
  • Total items across all colors ≤ 10^6
  • If fewer than 3 colors have positive counts, the answer is 0
  • Colors in each outfit list can appear in any order

Examples

Example 1

Input: build_outfits([3, 2, 2, 1])

Output: [2, [[0, 1, 2], [0, 1, 2]]]

Hints

Hint 1

What limits the total number of outfits: the total item count or the distribution across colors?

Hint 2

A color with a very large count can appear in every outfit at most once. How does this constrain the other colors?

Hint 3

Using a max-heap to repeatedly select the three most abundant colors ensures you don't exhaust any single color too quickly.