Find the maximum amount that can be collected by selling movie tickets
Given an integer N and an array seats[] where N is the number of people standing in a line to buy a movie ticket and seat[i] is the number of empty seats in the ith row of the movie theater. The task is to find the maximum amount a theater owner can make by selling movie tickets to N people. Price of a ticket is equal to the maximum number of empty seats among all the rows.
Example:
Input: seats[] = {1, 2, 4}, N = 3
Output: 9
Person |
Empty Seats |
Ticket Cost |
1 |
1 2 4 |
4 |
2 |
1 2 3 |
3 |
3 |
1 2 2 |
2 |
4 + 3 + 2 = 9
Input: seats[] = {2, 3, 5, 3}, N = 4
Output: 15
Approach: This problem can be solved by using a priority queue that will store the count of empty seats for every row and the maximum among them will be available at the top.
- Create an empty priority_queue q and traverse the seats[] array and insert all element into the priority_queue.
- Initialize two integer variable ticketSold = 0 and ans = 0 that will store the number of tickets sold and the total collection of the amount so far.
- Now check while ticketSold < N and q.top() > 0 then remove the top element from the priority_queue and update ans by adding top element of the priority queue. Also store this top value in a variable temp and insert temp – 1 back to the priority_queue.
- Repeat these steps until all the people have been sold the tickets and print the final result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxAmount( int M, int N, int seats[])
{
priority_queue< int > q;
for ( int i = 0; i < M; i++) {
q.push(seats[i]);
}
int ticketSold = 0;
int ans = 0;
while (ticketSold < N && q.top() > 0) {
ans = ans + q.top();
int temp = q.top();
q.pop();
q.push(temp - 1);
ticketSold++;
}
return ans;
}
int main()
{
int seats[] = { 1, 2, 4 };
int M = sizeof (seats) / sizeof ( int );
int N = 3;
cout << maxAmount(N, M, seats);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int [] seats = new int []{ 1 , 2 , 4 };
public static int maxAmount( int M, int N)
{
PriorityQueue<Integer> q =
new PriorityQueue<Integer>(Collections.reverseOrder());
for ( int i = 0 ; i < M; i++)
{
q.add(seats[i]);
}
int ticketSold = 0 ;
int ans = 0 ;
while (ticketSold < N && q.peek() > 0 )
{
ans = ans + q.peek();
int temp = q.peek();
q.poll();
q.add(temp - 1 );
ticketSold++;
}
return ans;
}
public static void main(String[] args)
{
int M = seats.length;
int N = 3 ;
System.out.print(maxAmount(M, N));
}
}
|
Python 3
import heapq
def maxAmount(M, N, seats):
q = seats
heapq._heapify_max(q)
ticketSold = 0
ans = 0
while ticketSold < N and q[ 0 ] > 0 :
ans + = q[ 0 ]
q[ 0 ] - = 1
if q[ 0 ] = = 0 :
break
heapq._heapify_max(q)
ticketSold + = 1
return ans
seats = [ 1 , 2 , 4 ]
M = len (seats)
N = 3
print (maxAmount(M, N, seats))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxAmount( int M, int N, int [] seats)
{
List< int > q = new List< int >();
for ( int i = 0; i < M; i++) {
q.Add(seats[i]);
}
q.Sort();
q.Reverse();
int ticketSold = 0;
int ans = 0;
while (ticketSold < N && q[0] > 0) {
ans = ans + q[0];
int temp = q[0];
q.RemoveAt(0);
q.Add(temp - 1);
q.Sort();
q.Reverse();
ticketSold++;
}
return ans;
}
static void Main()
{
int [] seats = { 1, 2, 4 };
int M = seats.Length;
int N = 3;
Console.WriteLine(maxAmount(N, M, seats));
}
}
|
Javascript
<script>
function maxAmount(M, N, seats)
{
let q = [];
for (let i = 0; i < M; i++) {
q.push(seats[i]);
}
q.sort( function (a, b){ return a - b});
q.reverse();
let ticketSold = 0;
let ans = 0;
while ((ticketSold < N) && (q[0] > 0)) {
ans = ans + q[0];
let temp = q[0];
q.shift();
q.push(temp - 1);
q.sort( function (a, b){ return a - b});
q.reverse();
ticketSold++;
}
return ans;
}
let seats = [ 1, 2, 4 ];
let M = seats.length;
let N = 3;
document.write(maxAmount(N, M, seats));
</script>
|
Time Complexity: O(N*logM) where N is tickets to be sold.
Explanation: In the condition of the while loop: ticketSold < N and q.top() >0, Value of N is determining the time required and here we need to maintain heap every time that needs O(Log(M))….so overall complexity will be O(N* Log(m)) where M is the size of given array.
Auxiliary Space: O(M) to store heap q.
Last Updated :
13 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...