#1648

Sell Diminishing-Valued Colored Balls

expert · 1145 · lc medium +32 · verified · 30.1% accepted · 1,153 likes · top 7%

Description

You have colored balls in an inventory array and a customer wanting orders balls. Each ball's sale price equals the current count of balls of that color. Selling the highest-valued balls first maximizes revenue. Return the maximum total revenue 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.

Code

1
2
3