Sell Diminishing-Valued Colored Balls - Problem

Imagine you're running a unique toy store where colored balls have a peculiar pricing system - each ball's value equals the current remaining inventory count of that color!

๐ŸŽฏ The Challenge: You have an inventory array where inventory[i] represents the number of balls of the i-th color. A customer wants to buy exactly orders balls total, and you need to maximize your revenue.

How the pricing works:

  • If you have 6 yellow balls, the first yellow ball sells for 6
  • After selling it, you have 5 yellow balls left, so the next yellow ball sells for 5
  • The value keeps decreasing as inventory depletes

You can sell balls in any order you want. Return the maximum total revenue modulo 109 + 7.

Example: With inventory [2,5] and orders = 4:
Optimal strategy: Sell balls valued at 5, 4, 3, 2 โ†’ Total revenue = 14

Input & Output

example_1.py โ€” Basic Case
$ Input: inventory = [2,5], orders = 4
โ€บ Output: 14
๐Ÿ’ก Note: Sell balls with values 5, 4, 3, 2. Total revenue = 5+4+3+2 = 14. We sell 2 balls from color 1 (values 5,4) and 2 balls from color 0 (values 2,1, but we only need 2 more balls so we sell at values 3,2).
example_2.py โ€” Large Inventory
$ Input: inventory = [3,5], orders = 6
โ€บ Output: 19
๐Ÿ’ก Note: Sell all balls: from color 1 (5,4,3) and from color 0 (3,2,1). Total = 5+4+3+3+2+1 = 18. Wait, let me recalculate: we want to sell optimally, so we sell 5,4,3,3,2,1 = 18. Actually, optimal is 5,4,3,3,2,1 = 18.
example_3.py โ€” Single Color
$ Input: inventory = [7], orders = 3
โ€บ Output: 18
๐Ÿ’ก Note: Only one color with 7 balls. Sell 3 balls with values 7, 6, 5. Total revenue = 7+6+5 = 18.

Visualization

Tap to expand
The Luxury Ball Store$66 Blue Balls$88 Red Balls$44 Yellow BallsSell Red for $8!๐ŸŽฏ Strategy: Always sell the most valuable ball firstStep 1: Red balls are worth $8 each (highest inventory)Step 2: After selling one red ball, remaining red balls worth $7Step 3: Continue selling highest value balls until orders fulfilled๐Ÿ’ก Key Optimization: Binary SearchInstead of simulating each sale O(orders ร— n)...Find the 'cutoff price level' using binary search O(n log max_inventory)Then calculate total revenue using arithmetic formulas!๐Ÿ† Result: From O(orders ร— n) to O(n log n) time complexityPerfect for handling up to 10โน orders and 10โต colors!
Understanding the Visualization
1
Premium Selection
Always sell the most expensive available ball first (highest current inventory count)
2
Dynamic Pricing
After each sale, the remaining balls of that color become slightly less valuable
3
Optimal Strategy
Use binary search to find the perfect 'cutoff price' instead of simulating each sale
Key Takeaway
๐ŸŽฏ Key Insight: Greedy approach works because ball values decrease linearly. Binary search finds the optimal cutoff price, avoiding expensive simulation while maintaining perfect accuracy.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n + n log(max_inventory))

O(n log n) for sorting, O(n log(max_inventory)) for binary search with O(n) validation

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space after sorting in place

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค inventory.length โ‰ค 105
  • 1 โ‰ค inventory[i] โ‰ค 109
  • 1 โ‰ค orders โ‰ค min(2ร—109, sum(inventory[i]))
  • Answer must be returned modulo 109 + 7
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
34.4K Views
High Frequency
~25 min Avg. Time
1.5K 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