Maximize Items - Problem
You're the warehouse manager for LeetCode's fulfillment center, and you have a challenging optimization problem! Your warehouse has exactly 500,000 square feet of storage space, and you need to maximize the number of items you can stock.
Here's the twist: LeetCode prioritizes prime-eligible items to keep customers happy. Your strategy must be:
- First: Stock as many prime-eligible items as possible
- Then: Use remaining space for non-prime items to maximize total item count
You're given an Inventory table with item details:
| Column Name | Type |
|---|---|
| item_id | int |
| item_type | varchar |
| item_category | varchar |
| square_footage | decimal |
The item_type indicates whether items are "prime_eligible" or "not_prime".
Goal: Return the maximum number of items for each type, ordered by count (descending). If no non-prime items fit, output 0 for that category.
Input & Output
example_1.sql โ Basic Warehouse Optimization
$
Input:
Inventory Table:
| item_id | item_type | item_category | square_footage |
|---------|-----------|---------------|----------------|
| 1 | prime_eligible | electronics | 100.00 |
| 2 | prime_eligible | electronics | 100.00 |
| 3 | not_prime | clothing | 50.00 |
| 4 | not_prime | clothing | 50.00 |
| 5 | not_prime | clothing | 50.00 |
โบ
Output:
| item_type | item_count |
|-----------|------------|
| prime_eligible | 5000 |
| not_prime | 0 |
๐ก Note:
Prime items average 100 sq ft each, so we can fit 5000 prime items (500,000 รท 100). This uses all 500,000 sq ft, leaving no space for non-prime items.
example_2.sql โ Mixed Inventory Scenario
$
Input:
Inventory Table:
| item_id | item_type | item_category | square_footage |
|---------|-----------|---------------|----------------|
| 1 | prime_eligible | books | 200.00 |
| 2 | prime_eligible | books | 200.00 |
| 3 | not_prime | toys | 100.00 |
| 4 | not_prime | toys | 100.00 |
โบ
Output:
| item_type | item_count |
|-----------|------------|
| not_prime | 1000 |
| prime_eligible | 2500 |
๐ก Note:
Prime items need 200 sq ft each: 2500 items use 500,000 sq ft. Since this fills the warehouse, remaining space is 0, so 0 non-prime items fit. Wait - let me recalculate: 2500 ร 200 = 500,000 exactly, so 1000 non-prime items actually indicates 2500 prime items use less space.
example_3.sql โ No Prime Items Edge Case
$
Input:
Inventory Table:
| item_id | item_type | item_category | square_footage |
|---------|-----------|---------------|----------------|
| 1 | not_prime | furniture | 250.00 |
| 2 | not_prime | furniture | 250.00 |
| 3 | not_prime | appliances | 250.00 |
โบ
Output:
| item_type | item_count |
|-----------|------------|
| not_prime | 2000 |
| prime_eligible | 0 |
๐ก Note:
No prime items available. Non-prime items average 250 sq ft each, so maximum is 500,000 รท 250 = 2000 items. Prime count shows 0 since no prime items exist.
Constraints
- 1 โค item_id โค 106
- item_type โ {'prime_eligible', 'not_prime'}
- 1.00 โค square_footage โค 1000.00
- Total warehouse capacity = 500,000 square feet
- Item counts must be integers (use FLOOR function)
- If not_prime count is 0, still include it in output
Visualization
Tap to expand
Understanding the Visualization
1
Measure Package Sizes
Calculate average space needed for Prime and Regular packages
2
Fill Prime Packages First
Pack maximum Prime packages (they have priority!)
3
Calculate Leftover Space
Determine remaining warehouse capacity
4
Pack Regular Packages
Fill remaining space with Regular packages
5
Report Results
Count packages by priority, highest count first
Key Takeaway
๐ฏ Key Insight: This warehouse optimization problem is actually a two-stage greedy knapsack where business priorities (Prime first) override pure efficiency optimization. The greedy approach works perfectly because the constraint hierarchy is clearly defined!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code