Sell Diminishing-Valued Colored Balls - Solution & Explanation
Problem Statement
You have an inventory of different colored balls, and there is a customer that wants orders balls of any color.
The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your inventory. For example, if you own 6 yellow balls, the customer would pay 6 for the first yellow ball. After the transaction, there are only 5 yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls decreases as you sell more to the customer).
You are given an integer array, inventory, where inventory[i] represents the number of balls of the ith color that you initially own. You are also given an integer orders, which represents the total number of balls that the customer wants. You can sell the balls in any order.
Return the maximum total value that you can attain after selling orders colored balls. As the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: inventory = [2,5], orders = 4 Output: 14 Explanation: Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3). The maximum total value is 2 + 5 + 4 + 3 = 14.
Example 2:
Input: inventory = [3,5], orders = 6 Output: 19 Explanation: Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2). The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.
Constraints:
1 <= inventory.length <= 1051 <= inventory[i] <= 1091 <= orders <= min(sum(inventory[i]), 109)
Approach Overview
Problem Overview: You are given an inventory where each number represents how many balls of a color you have. The value of a ball equals the current count of that color, and every sale decreases its value by 1. The goal is to sell exactly orders balls while maximizing total profit.
Approach 1: Greedy with Priority Queue (Heap) (Time: O(orders log n), Space: O(n))
This approach always sells the most valuable ball available. Store all inventory counts in a max heap. Each step removes the largest value, adds it to profit, decreases it by one, and pushes it back if the value is still positive. The greedy idea works because selling higher-valued balls earlier always increases total revenue. A heap ensures efficient retrieval of the current maximum. This approach is intuitive and easy to implement using a heap (priority queue), though it can be slower when orders is very large because every sale requires a heap operation.
Approach 2: Binary Search + Arithmetic Series (Time: O(n log maxValue), Space: O(1))
A more optimized strategy avoids simulating every sale. Instead, observe that selling balls from a color produces a decreasing sequence like k + (k-1) + (k-2) .... Use binary search to find a threshold value x such that selling all balls valued above x satisfies or nearly satisfies the required number of orders. Once the threshold is found, compute the profit using arithmetic series formulas instead of iterating per sale. For each inventory value greater than x, sum the range value ... x+1. Remaining orders at value x are added afterward. This reduces the work dramatically and is the typical optimal solution in interviews.
The solution also benefits from sorting or iterating the array of inventory counts while calculating how many balls exist above the threshold. The mathematical insight transforms a potentially billions-step simulation into a few arithmetic operations.
Recommended for interviews: The binary search approach is what interviewers usually expect because it demonstrates strong reasoning with greedy strategy and arithmetic series optimization. Implementing the heap solution first shows correct intuition about always selling the highest-value ball, but recognizing the need to aggregate ranges with math separates a good solution from a great one.
Approach 1: Approach 1: Greedy with Priority Queue
This approach uses a max-heap (priority queue) to always sell the highest valued balls first.
Steps:
- Insert all inventory values into a max-heap.
- While orders remain, pop the largest value from the heap, sell it, and push the remaining (value - 1) back into the heap.
- Keep track of the total value sold and reduce the number of orders after each sale.
- Finally, return the total value modulo
10^9 + 7.
The algorithm uses a max-heap to store the values of colored balls in descending order. It repeatedly pops the highest value, sells a number of balls equal to the difference between the current max and the next max, adjusts the orders remaining, and calculates the total value gained. The heap is adjusted accordingly to reflect the new ball count.
Code
Python
JavaScript
Complexity
Time Complexity: O(n+mlog n) where n is the number of colors and m is the number of orders.
Space Complexity: O(n) for storing the heap and other auxiliary structures.
Approach 2: Approach 2: Binary Search
This approach uses binary search to determine the threshold at which we should stop selling each ball to ensure maximum profit.
Steps:
- Sort the inventory values in descending order.
- Apply binary search to find the maximum price we can sell at while still fulfilling the orders.
- On each iteration of the binary search, calculate the possible number of balls we can sell at a given price.
- Adjust the search range based on whether we can fulfill the order or not.
This C++ solution determines the threshold price at which to stop selling balls using a binary search over the maximum number of balls that can be sold. The sorted inventory helps easily determine levels at which ball count must diminish. The search seeks to balance ball sale count against the maximum price achievable.
Complexity
Time Complexity: O(nlog m) where n is the number of colors and m is the maximum ball count.
Space Complexity: O(1) aside from input storage.
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Approach 1: Greedy with Priority Queue | Time Complexity: O(n+mlog n) where n is the number of colors and m is the number of orders. |
| Approach 2: Binary Search | Time Complexity: O(nlog m) where n is the number of colors and m is the maximum ball count. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy with Max Heap | O(orders log n) | O(n) | Good for understanding the greedy idea and when orders are relatively small |
| Binary Search + Arithmetic Series | O(n log maxValue) | O(1) | Optimal solution when orders can be very large and simulation would be too slow |
Video Solution
Leetcode 1648 Sell Diminishing Valued Colored Balls • Fraz • 11,086 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Sell Diminishing-Valued Colored Balls easy or hard?
Sell Diminishing-Valued Colored Balls Python/Java solution
How to solve Sell Diminishing-Valued Colored Balls in O(n)?
What is the best approach for Sell Diminishing-Valued Colored Balls?
Is Sell Diminishing-Valued Colored Balls asked at Google/Amazon/Meta?
What data structure is used in Sell Diminishing-Valued Colored Balls?
What is the time complexity of Sell Diminishing-Valued Colored Balls?
Ready to solve this problem?
Practice Sell Diminishing-Valued Colored Balls with our built-in code editor and test cases.
Practice on FleetCode