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:

  1. First: Stock as many prime-eligible items as possible
  2. Then: Use remaining space for non-prime items to maximize total item count

You're given an Inventory table with item details:

Column NameType
item_idint
item_typevarchar
item_categoryvarchar
square_footagedecimal

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
Warehouse Optimization: Prime First Strategy๐Ÿญ Total Warehouse: 500,000 sq ftAvailable Storage SpaceMust optimize for maximum item count๐ŸŽฏ PRIME ELIGIBLE (Priority)โ€ข Calculate: FLOOR(500,000 รท avg_prime_space)โ€ข Allocate maximum prime items firstโ€ข Track space used: count ร— avg_space1๐Ÿ“ CalculateRemainingSpace500k - used_space2๐Ÿ“ฆ NOT PRIME (Secondary)โ€ข Use remaining space onlyโ€ข Calculate: FLOOR(remaining รท avg_nonprime_space)โ€ข Maximize items in leftover space3๐Ÿ“Š Final ResultsOrder by count(DESC)Highest first4๐Ÿ’ก Key Algorithm Insight:This is a constrained knapsack problem - maximize items with priority constraintGreedy approach works because we prioritize by business rules, not just efficiencyTime: O(n) | Space: O(1) - Single pass with constant extra storage
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!
Asked in
Amazon 85 Google 42 Meta 38 Microsoft 31
52.3K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen