Sell Diminishing-Valued Colored Balls Problem
Description
LeetCode Problem 1648.
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 i^th 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 10^9+ 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 <= 10^51 <= inventory[i] <= 10^91 <= orders <= min(sum(inventory[i]), 10^9)
Solution
Approach: Binary Search the Lowest Price, Then Sum Ranges
Each sale should take a ball from a color with the most balls left, since that ball is worth the most right now. If we keep doing this, the stocks are worn down from the top like a water level going down: in the end there is a price t such that every ball worth more than t was sold, and the remaining orders were sold at price t.
orders can be 109, so we find t directly instead of selling one ball at a time. Selling everything worth more than t takes sum(max(0, a - t)) sales, which gets smaller as t grows. Binary search for the smallest t with that sum <= orders.
Then:
- For each color with
a > t, we sold the valuesa, a-1, ..., t+1. That sum is(a + t + 1) * (a - t) / 2. Subtract thea - tballs fromorders. - The orders left are sold at price
teach. There are fewer of them than colors that still havetballs, otherwiset - 1would also have worked, so addingorders * tis right.
The sums are large: a single color can bring in about 5 · 1017, so use 64-bit integers and take the modulo as you add (in JavaScript, use BigInt for this part). Never take the modulo of the counts used in the binary search, only of the money.
Complexity: O(n log M) time, where M is the largest stock (about 30 rounds of binary search over n colors). O(1) extra space.
class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
mod = 10**9 + 7
# Find the smallest price t such that selling every ball worth more than t
# takes at most `orders` sales.
lo, hi = 0, max(inventory)
while lo < hi:
mid = (lo + hi) // 2
if sum(a - mid for a in inventory if a > mid) <= orders:
hi = mid
else:
lo = mid + 1
t = lo
ans = 0
for a in inventory:
if a > t:
ans += (a + t + 1) * (a - t) // 2 # sells a, a-1, ..., t+1
orders -= a - t
# The orders left are fewer than the colors that still have t balls: sell them at t.
ans += orders * t
return ans % mod