Leetcode Problem 1648. Sell Diminishing-Valued Colored Balls

1648. Sell Diminishing-Valued Colored Balls

Leetcode Solutions

Sort + Greedy Approach

  1. Sort the inventory array in descending order.
  2. Initialize the total profit to 0 and the count of balls at the current value to 1.
  3. 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.
  4. Return the total profit modulo 1e9+7.
UML Thumbnail

Binary Search Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...