Maximize the profit after selling the tickets
Last Updated :
28 Jun, 2022
Given 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++14
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int seats[], int k, int n)
{
priority_queue< int > pq;
for ( int i = 0; i < k; i++)
pq.push(seats[i]);
int profit = 0;
int c = 0;
while (c < n) {
int top = pq.top();
pq.pop();
if (top == 0)
break ;
profit = profit + top;
pq.push(top - 1);
c++;
}
return profit;
}
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;
}
|
Java
import java.util.*;
class GFG {
static int maxProfit( int seats[], int k, int n)
{
PriorityQueue<Integer> pq;
pq = new PriorityQueue<>(Collections.reverseOrder());
for ( int i = 0 ; i < k; i++)
pq.add(seats[i]);
int profit = 0 ;
int c = 0 ;
while (c < n)
{
int top = pq.remove();
if (top == 0 )
break ;
profit = profit + top;
pq.add(top - 1 );
c++;
}
return profit;
}
public static void main(String args[])
{
int seats[] = { 2 , 3 , 4 , 5 , 1 };
int k = seats.length;
int n = 6 ;
System.out.println(maxProfit(seats, k ,n));
}
}
|
Python3
import heapq
def maxProfit(seats, k, n):
pq = seats
heapq._heapify_max(pq)
profit = 0
while n > 0 :
profit + = pq[ 0 ]
pq[ 0 ] - = 1
if pq[ 0 ] = = 0 :
break
heapq._heapify_max(pq)
n - = 1
return profit
seats = [ 2 , 3 , 4 , 5 , 1 ]
k = len (seats)
n = 6
print (maxProfit(seats, k, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxProfit( int [] seats, int k, int n)
{
List< int > pq = new List< int >();
for ( int i = 0; i < k; i++)
pq.Add(seats[i]);
int profit = 0;
int c = 0;
while (c < n)
{
pq.Sort();
pq.Reverse();
int top = pq[0];
pq.RemoveAt(0);
if (top == 0)
break ;
profit = profit + top;
pq.Add(top - 1);
c++;
}
return profit;
}
static void Main()
{
int [] seats = { 2, 3, 4, 5, 1 };
int k = seats.Length;
int n = 6;
Console.Write(maxProfit(seats, k, n));
}
}
|
Javascript
<script>
function maxProfit(seats, k, n)
{
let priorityQueue = counter.map((item) => item);
let profit = 0;
while (n != 0)
{
priorityQueue.sort((a,b) => b - a);
let top = priorityQueue[0];
priorityQueue.shift();
if (top == 0)
break ;
profit = profit + top;
priorityQueue.push(top - 1);
n--;
}
return profit;
}
let seats = [ 2, 3, 4, 5, 1 ];
let k = seats.length;
let n = 6;
document.write(maxProfit(seats, k, n));
</script>
|
Time complexity: O(n*log(n))
Auxiliary Space: O(n)
Sliding Window approach:
The problem can also be solved using the sliding window technique.
- For each person we need to sell ticket that has the maximum price and decrement its value by 1.
- Sort the array seats.
- Maintain two pointers pointing at the current maximum and next maximum number of seats .
- We iterate till our n>0 and there is a second largest element in the array.
- In each iteration if seats[i] > seats[j] ,we add the value at seats[i] ,min(n, i-j) times to our answer and decrement the value at ith index else we find j such that seats[j]<seats[i]. If there is no such j we break.
- If at the end of iteration our n>0 and seats[i]!=0 we add seats[i] till n>0 and seats[i]!=0.
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int seats[], int k, int n)
{
sort(seats,seats+k);
int ans = 0;
int i = k - 1;
int j = k - 2;
while (n > 0 && j >= 0) {
if (seats[i] > seats[j]) {
ans = ans + min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
else {
while (j >= 0 && seats[j] == seats[i])
j--;
if (j < 0)
break ;
ans = ans + min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
}
while (n > 0 && seats[i] != 0) {
ans = ans + min(n, k) * seats[i];
n -= k;
seats[i]--;
}
return ans;
}
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;
}
|
Java
import java.util.Arrays;
class GFG {
static int maxProfit( int seats[], int k, int n)
{
Arrays.sort(seats, 0 , k);
int ans = 0 ;
int i = k - 1 ;
int j = k - 2 ;
while (n > 0 && j >= 0 ) {
if (seats[i] > seats[j]) {
ans = ans + Math.min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
else {
while (j >= 0 && seats[j] == seats[i])
j--;
if (j < 0 )
break ;
ans = ans + Math.min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
}
while (n > 0 && seats[i] != 0 ) {
ans = ans + Math.min(n, k) * seats[i];
n -= k;
seats[i]--;
}
return ans;
}
public static void main(String[] args)
{
int seats[] = { 2 , 3 , 4 , 5 , 1 };
int k = seats.length;
int n = 6 ;
System.out.println(maxProfit(seats, k, n));
}
}
|
Python3
def maxProfit(seats,k, n):
seats.sort()
ans = 0
i = k - 1
j = k - 2
while (n > 0 and j > = 0 ):
if (seats[i] > seats[j]):
ans = ans + min (n, (i - j)) * seats[i]
n = n - (i - j)
seats[i] - = 1
else :
while (j > = 0 and seats[j] = = seats[i]):
j - = 1
if (j < 0 ):
break
ans = ans + min (n, (i - j)) * seats[i]
n = n - (i - j)
seats[i] - = 1
while (n > 0 and seats[i] ! = 0 ):
ans = ans + min (n, k) * seats[i]
n - = k
seats[i] - = 1
return ans
seats = [ 2 , 3 , 4 , 5 , 1 ]
k = len (seats)
n = 6
print (maxProfit(seats, k, n))
|
C#
using System;
class GFG {
static int maxProfit( int []seats, int k, int n)
{
Array.Sort(seats, 0, k);
int ans = 0;
int i = k - 1;
int j = k - 2;
while (n > 0 && j >= 0) {
if (seats[i] > seats[j]) {
ans = ans + Math.Min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
else {
while (j >= 0 && seats[j] == seats[i])
j--;
if (j < 0)
break ;
ans = ans + Math.Min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
}
while (n > 0 && seats[i] != 0) {
ans = ans + Math.Min(n, k) * seats[i];
n -= k;
seats[i]--;
}
return ans;
}
public static void Main(String[] args)
{
int []seats = { 2, 3, 4, 5, 1 };
int k = seats.Length;
int n = 6;
Console.Write(maxProfit(seats, k, n));
}
}
|
Javascript
<script>
function maxProfit(seats,k, n)
{
seats.sort();
var ans = 0;
var i = k - 1;
var j = k - 2;
while (n > 0 && j >= 0) {
if (seats[i] > seats[j]) {
ans = ans + Math.min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
else {
while (j >= 0 && seats[j] == seats[i])
j--;
if (j < 0)
break ;
ans = ans + Math.min(n, (i - j)) * seats[i];
n = n - (i - j);
seats[i]--;
}
}
while (n > 0 && seats[i] != 0) {
ans = ans + Math.min(n, k) * seats[i];
n -= k;
seats[i]--;
}
return ans;
}
var seats = [2, 3, 4, 5, 1];
var k = seats.length;
var n = 6;
document.write(maxProfit(seats, k, n));
</script>
|
Time Complexity: O(k logk), where k is the size of the given array of seats
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...