Build Three-Color Outfits from Limited Inventory
Heaps, Sorting & SearchAuto-graded · 19 test casesLast 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:
- The maximum number of complete outfits you can create
- 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) ≤ 10000 ≤ 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires finding the maximum number of 3-item outfits where each outfit uses three different colors, then constructing a valid assignment for up to 10 outfits.
Key Insight: The maximum number of outfits is constrained by two factors:
- Total capacity: With
Stotal items, you can make at most⌊S/3⌋outfits - Distribution constraint: If one color dominates with count
M, it can appear in at mostMoutfits (once per outfit). The remainingS - Mitems must fill the other two slots across all outfits, requiring at least2kitems forkoutfits.
Therefore: max_outfits = min(⌊S/3⌋, ⌊(S - M)/2⌋) where M is the largest color count.
Why this formula works:
- If the largest color has
Mitems and we makekoutfits, it contributes at mostmin(M, k)items - The other colors must contribute
3k - min(M, k)items - When
M > k, the dominant color appears in every outfit, and the remaining colors must provide2kitems
Construction Strategy:
Use a max-heap to greedily select colors:
- Maintain a heap of (negative_count, color_index) for all colors with remaining items
- For each outfit, pop the three most abundant colors
- Decrement their counts and push back if they still have items remaining
This greedy approach ensures we don't exhaust any single color prematurely, maintaining balance in the distribution.
Time Complexity: O(C log C) where C is the number of colors
- Computing the maximum: O(C) to sum and find max
- Heapify: O(C)
- Building 10 outfits: O(10 × log C) for heap operations
Space Complexity: O(C) for the heap
import heapq
from typing import List
def build_outfits(inventory: List[int]) -> List[object]:
total_items = sum(inventory)
max_single_color = max(inventory, default=0)
# Maximum outfits is limited by two factors:
# 1. Total items divided by 3 (each outfit needs 3 items)
# 2. Items not in the largest color divided by 2 (the largest color
# can appear at most once per outfit, so other colors need to pair)
max_outfits = min(total_items // 3, (total_items - max_single_color) // 2)
# Use a max-heap to greedily select the three most abundant colors
# for each outfit. Store negative counts for max-heap behavior.
heap = [(-count, idx) for idx, count in enumerate(inventory) if count > 0]
heapq.heapify(heap)
outfit_assignments = []
for _ in range(min(10, max_outfits)):
# Extract the three colors with the most remaining items
top_three = [heapq.heappop(heap) for _ in range(3)]
outfit_assignments.append([color_idx for _, color_idx in top_three])
# Decrement counts and push back if items remain
for neg_count, color_idx in top_three:
if neg_count + 1 < 0: # Still items left after using one
heapq.heappush(heap, (neg_count + 1, color_idx))
return [max_outfits, outfit_assignments]