Calculate Revenue from Highest-Inventory VM Sales
IBMSoftware Engineer · Online AssessmentLast updated
Sell each VM unit at its current inventory count, always choose a type with maximum inventory, and compute total revenue safely.
A provider sells units from several VM inventory types. The price of one unit equals that type's current remaining inventory immediately before the sale. Each customer buys one unit from a type with the greatest current inventory, then that type's inventory decreases by one.
Function Signature
vm_revenue(inventory: list[int], customers: int) -> int
Output
Return total revenue after serving exactly customers customers. Any type tied for greatest inventory may be chosen; the total revenue is unchanged by that tie choice. Types with zero inventory cannot be sold.
Constraints
1 <= len(inventory) <= 200000.0 <= inventory[i] <= 1000000000.0 <= customers <= 200000.customers <= sum(inventory).- Use an integer type large enough for the total revenue, which may exceed a signed 32-bit integer.
- Do not mutate the input array.
Examples
Input: inventory = [1,2,4], customers = 4
Output: 11
The four sale prices are 4, 3, 2, and 2.
Input: inventory = [2,2], customers = 3
Output: 5
Input: inventory = [0,3], customers = 0
Output: 0
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.