Maximize the profit after selling the tickets
Given an array seats[] where seat[i] is the number of vacant seats in the ith row in a stadium for a cricket match. There are N people in a queue waiting to buy the tickets. Each seat costs equal to the number of vacant seats in the row it belongs to. The task is to maximize the profit by selling the tickets to N people.
Examples:
Input: seats[] = {2, 1, 1}, N = 3
Output: 4
Person 1: Sell the seat in the row with
2 vacant seats, seats = {1, 1, 1}
Person 2: All the rows have 1 vacant
seat each, seats[] = {0, 1, 1}
Person 3: seats[] = {0, 0, 1}
Input: seats[] = {2, 3, 4, 5, 1}, N = 6
Output: 22
Approach: In order to maximize the profit, the ticket must be for the seat in a row which has the maximum number of vacant seats and the number of vacant seats in that row will be decrement by 1 as one of the seats has just been sold. All the persons can be sold a seat ticket until there are vacant seats. This can be computed efficiently with the help of a priority_queue.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximized profit int maxProfit(int seats[], int k, int n) { // Push all the vacant seats // in a priority queue priority_queue<int> pq; for (int i = 0; i < k; i++) pq.push(seats[i]); // To store the maximized profit int profit = 0; // To count the people that // have been sold a ticket int c = 0; while (c < n) { // Get the maximimum number of // vacant seats for any row int top = pq.top(); // Remove it from the queue pq.pop(); // If there are no vacant seats if (top == 0) break; // Update the profit profit = profit + top; // Push the updated status of the // vacant seats in the current row pq.push(top - 1); // Update the count of persons c++; } return profit; } // Driver code int main() { int seats[] = { 2, 3, 4, 5, 1 }; int k = sizeof(seats) / sizeof(int); int n = 6; cout << maxProfit(seats, k, n); return 0; } |
22
Recommended Posts:
- Maximize the profit by selling at-most M products
- Find the maximum amount that can be collected by selling movie tickets
- Maximum profit after buying and selling the stocks
- Stock Buy Sell to Maximize Profit
- Calculate the loss incurred in selling the given items at discounted price
- Maximize the sum of arr[i]*i
- Maximize arr[j] - arr[i] + arr[l] - arr[k], such that i < j < k < l
- Maximize value of (a[i]+i)*(a[j]+j) in an array
- Maximize value of (arr[i] - i) - (arr[j] - j) in an array
- Maximize the number of subarrays with XOR as zero
- Rearrange an array to maximize i*arr[i]
- Maximize the median of an array
- Given an array and three numbers, maximize (x * a[i]) + (y * a[j]) + (z * a[k])
- Maximize elements using another array
- Maximize array sum after K negations | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

