Optimal Dynamic Flight Seating Revenue
Ixigo is optimizing its dynamic pricing model for flight seats. For a popular flight, the airline pre-allocates a certain number of seats and takes group bookings. To maximize revenue, the airline employs a dynamic pricing strategy: each time a block of seats is sold to a group, the base price for all subsequent seat sales increases by a fixed increment. Your task is to determine the maximum total revenue the airline can achieve by strategically accepting and ordering group bookings. This problem is critical for maximizing profitability on high-demand routes while managing limited inventory.
Implement a function calculateMaxRevenue that determines the maximum possible revenue the airline can generate.
The function should:
increment.Function Signature:
function calculateMaxRevenue(totalSeats: number, basePrice: number, increment: number, groups: number[][]): number
totalSeats: An integer representing the total number of seats available on the flight.basePrice: An integer representing the initial price for a single seat before any sales.increment: An integer representing the amount by which the dynamic seat price increases after each successful group booking.groups: A 2D array of integers, where each inner array [k_i, p_i] represents a group booking request:
k_i: The number of seats requested by group i.p_i: The maximum price per seat group i is willing to pay.increment applies to the per-seat price.totalSeats becomes 0, no more groups can be accepted.basePrice and increment are always positive. k_i and p_i are always positive.Examples
Example 1
Input:
totalSeats = 10, basePrice = 100, increment = 10, groups = [[3, 130], [5, 110], [2, 120]]
Output:
1080
Note:
availableSeats = 10, currentPrice = 100, totalRevenue = 0.p_i ascending, then k_i ascending): [[5, 110], [2, 120], [3, 130]].[5, 110]:
5 * 100 = 500. Max allowed: 5 * 110 = 550. 500 <= 550. Accept.availableSeats = 10 - 5 = 5. totalRevenue = 500.currentPrice = 100 + 10 = 110.[2, 120]:
2 * 110 = 220. Max allowed: 2 * 120 = 240. 220 <= 240. Accept.availableSeats = 5 - 2 = 3. totalRevenue = 500 + 220 = 720.currentPrice = 110 + 10 = 120.[3, 130]:
3 * 120 = 360. Max allowed: 3 * 130 = 390. 360 <= 390. Accept.availableSeats = 3 - 3 = 0. totalRevenue = 720 + 360 = 1080.currentPrice = 120 + 10 = 130.
Final totalRevenue = 1080.Example 2
Input:
totalSeats = 5, basePrice = 50, increment = 5, groups = [[2, 50], [3, 60], [1, 40]]
Output:
265
Note:
availableSeats = 5, currentPrice = 50, totalRevenue = 0.[[1, 40], [2, 50], [3, 60]].[1, 40]:
1 * 50 = 50. Max allowed: 1 * 40 = 40. 50 > 40. Reject.availableSeats = 5. totalRevenue = 0. currentPrice remains 50.[2, 50]:
2 * 50 = 100. Max allowed: 2 * 50 = 100. 100 <= 100. Accept.availableSeats = 5 - 2 = 3. totalRevenue = 100.currentPrice = 50 + 5 = 55.[3, 60]:
3 * 55 = 165. Max allowed: 3 * 60 = 180. 165 <= 180. Accept.availableSeats = 3 - 3 = 0. totalRevenue = 100 + 165 = 265.currentPrice = 55 + 5 = 60.
Final totalRevenue = 265.Example 3
Input:
totalSeats = 3, basePrice = 100, increment = 10, groups = [[2, 120], [2, 110], [1, 150]]
Output:
310
Note:
availableSeats = 3, currentPrice = 100, totalRevenue = 0.[[2, 110], [2, 120], [1, 150]] (Sorted by p_i ascending, then k_i ascending).[2, 110]:
2 * 100 = 200. Max allowed: 2 * 110 = 220. 200 <= 220. Accept.availableSeats = 3 - 2 = 1. totalRevenue = 200.currentPrice = 100 + 10 = 110.[2, 120]:
availableSeats = 1. Not enough seats for 2. Reject.availableSeats = 1. totalRevenue = 200. currentPrice remains 110.[1, 150]:
availableSeats = 1. Enough seats.1 * 110 = 110. Max allowed: 1 * 150 = 150. 110 <= 150. Accept.availableSeats = 1 - 1 = 0. totalRevenue = 200 + 110 = 310.currentPrice = 110 + 10 = 120.
Final totalRevenue = 310.Constraints
1 <= totalSeats <= 10^51 <= basePrice <= 10^61 <= increment <= 10^61 <= groups.length <= 10^51 <= k_i <= totalSeats1 <= p_i <= 10^6number type, which uses double-precision floating-point format, can precisely represent integers up to 2^53 - 1, sufficient for 10^16).O(M log M) or O(M)).Loading editor...