Leetcode Problem 1648. Sell Diminishing-Valued Colored Balls
1648. Sell Diminishing-Valued Colored Balls
Leetcode Solutions
Sort + Greedy Approach
Sort the inventory array in descending order.
Initialize the total profit to 0 and the count of balls at the current value to 1.
Iterate over the sorted inventory.
a. Calculate the difference between the current value and the next lower value.
b. If the orders are enough to cover the difference times the count, add the profit for this group to the total profit and subtract the orders.
c. If the orders are not enough to cover the entire group, calculate how many times we can sell at the current value and how many balls we can sell at the value one less.
d. Add the profit for these remaining orders to the total profit and set orders to 0.