给定一个整数N和一个数组座位 [] ,其中N是排队购买电影票的人数,座位 [i]是电影院第i排的空座位数。任务是找到剧院老板通过向N人出售电影票可以赚取的最大金额。一张票的价格等于所有行中空座位的最大数量。
例子:
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
方法:这个问题可以通过使用优先队列来解决,该队列将存储每行空座位的数量,其中最大的将在顶部可用。
- 创建一个空的priority_queue q并遍历seats[]数组并将所有元素插入priority_queue。
- 初始化两个整数变量ticketSold = 0和ans = 0 ,它们将存储到目前为止售出的门票数量和总收藏金额。
- 现在检查 while ticketSold < N and q.top() > 0然后从 priority_queue 中删除顶部元素并通过添加优先级队列的顶部元素来更新 ans。还将这个最高值存储在变量temp 中,并将temp – 1插入到 priority_queue 中。
- 重复这些步骤,直到所有的人都卖完票并打印最终结果。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum amount
// that can be collected by selling tickets
int maxAmount(int M, int N, int seats[])
{
// Priority queue that stores
// the count of empty seats
priority_queue q;
// Insert each array element
// into the priority queue
for (int i = 0; i < M; i++) {
q.push(seats[i]);
}
// To store the total
// number of tickets sold
int ticketSold = 0;
// To store the total amount
// of collection
int ans = 0;
// While tickets sold are less than N
// and q.top > 0 then update the collected
// amount with the top of the priority
// queue
while (ticketSold < N && q.top() > 0) {
ans = ans + q.top();
int temp = q.top();
q.pop();
q.push(temp - 1);
ticketSold++;
}
return ans;
}
// Driver code
int main()
{
int seats[] = { 1, 2, 4 };
int M = sizeof(seats) / sizeof(int);
int N = 3;
cout << maxAmount(N, M, seats);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int[] seats = new int[]{ 1, 2, 4 };
// Function to return the maximum amount
// that can be collected by selling tickets
public static int maxAmount(int M, int N)
{
// Priority queue that stores
// the count of empty seats
PriorityQueue q =
new PriorityQueue(Collections.reverseOrder());
// Insert each array element
// into the priority queue
for (int i = 0; i < M; i++)
{
q.add(seats[i]);
}
// To store the total
// number of tickets sold
int ticketSold = 0;
// To store the total amount
// of collection
int ans = 0;
// While tickets sold are less than N
// and q.top > 0 then update the collected
// amount with the top of the priority
// queue
while (ticketSold < N && q.peek() > 0)
{
ans = ans + q.peek();
int temp = q.peek();
q.poll();
q.add(temp - 1);
ticketSold++;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int M = seats.length;
int N = 3;
System.out.print(maxAmount(M, N));
}
}
// This code is contributed by Sanjit_Prasad
Python 3
# Python 3 implementation of the approach
# Function to return the maximum amount
# that can be collected by selling tickets
def maxAmount(M, N, seats):
# Priority queue that stores
# the count of empty seats
q = []
# Insert each array element
# into the priority queue
for i in range(M):
q.append(seats[i])
# To store the total
# number of tickets sold
ticketSold = 0
# To store the total amount
# of collection
ans = 0
# While tickets sold are less than N
# and q.top > 0 then update the collected
# amount with the top of the priority
# queue
q.sort(reverse = True)
while (ticketSold < N and q[0] > 0):
ans = ans + q[0]
temp = q[0]
q = q[1:]
q.append(temp - 1)
q.sort(reverse = True)
ticketSold += 1
return ans
# Driver code
if __name__ == '__main__':
seats = [1, 2, 4]
M = len(seats)
N = 3
print(maxAmount(N, M, seats))
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the maximum amount
// that can be collected by selling tickets
static int maxAmount(int M, int N, int[] seats)
{
// Priority queue that stores
// the count of empty seats
List q = new List();
// Insert each array element
// into the priority queue
for (int i = 0; i < M; i++) {
q.Add(seats[i]);
}
q.Sort();
q.Reverse();
// To store the total
// number of tickets sold
int ticketSold = 0;
// To store the total amount
// of collection
int ans = 0;
// While tickets sold are less than N
// and q.top > 0 then update the collected
// amount with the top of the priority
// queue
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;
}
// Driver code
static void Main()
{
int[] seats = { 1, 2, 4 };
int M = seats.Length;
int N = 3;
Console.WriteLine(maxAmount(N, M, seats));
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
9