FastPrepMaximize Revenue from Decreasing Inventory

Maximize Revenue from Decreasing Inventory

Chubb logoChubb● MediumNEW GRADOA
Learn

Problem statement

A shop stocks several item types. The integer quantities[i] is the remaining inventory of type i. Selling one item from a type earns its current remaining quantity, then reduces that quantity by one.

Exactly customers customers each buy one item. Choose the item type for every sale to maximize total revenue. Return that maximum revenue.

Function

maximizeRevenue(quantities: int[], customers: int) → long

Examples

Example 1

quantities = [2,5]customers = 4return = 14

Sell from remaining quantities 5, 4, 3, and 2 for total revenue 14.

Example 2

quantities = [3,3]customers = 3return = 8

The best sale prices are 3, 3, and 2.

Constraints

  • 1 <= quantities.length <= 100000.
  • 1 <= quantities[i] <= 1000000.
  • 1 <= customers <= 1000000.
  • customers <= sum(quantities).
  • The answer fits in a signed 64-bit integer.

More Chubb problems

See Chubb hiring insights
public long maximizeRevenue(int[] quantities, int customers) {
    // Write your solution here.
}
quantities[2,5]
customers4
expected14
Checking account…