2

In a theatre show there are N seats [1,2,3,...N] each set at a different price such that the ith ticket costs A[i]. People come in group and want to sit together. The indexes of the array B indicate the respective group sizes. With an optimal seating plan, determine the maximum profit the owner can make.

Eg:

N=6;

A = [9, 2, 5, 1, 2, 3]

B = [2, 3]

Ans: The array A can be divided such that 9 + 2 + 5 = 16 and 2+3=5. Therefore the cost of the ticket sold can be 16+5=21

Kartik Thakur
  • 57
  • 1
  • 7
  • 2
    What have you tried? Where are you facing an issue? – Abhinav Mathur Aug 30 '22 at 04:44
  • 2
    What are the limits on N, the prices, and the group sizes? – user3386109 Aug 30 '22 at 05:05
  • @AbhinavMathur I am thinking of applying sliding window approach but i have no clue how to do that for more than one window sliding while maintaining the max value for each. – Kartik Thakur Aug 30 '22 at 06:08
  • @user3386109 you can assume its not that big of numbers, I just needed one approach to solve this kind of problems and I would appreciate your help. – Kartik Thakur Aug 30 '22 at 06:09
  • 2
    One approach is to try all of the permutations of array B. In the example, there are only two permutations: [2,3] and [3,2]. Then try putting the first group at every valid position. A valid position is one that leaves enough room for the other groups. A sliding window can be used to quickly compute the profit for each position. Memoization can be used because there will be common subproblems to solve. – user3386109 Aug 30 '22 at 06:59
  • @user3386109 The larger groups will always be more profitable to place first. However, in my solution I did not think to leave room for other groups. Good thinking – Fire 'N Lightnin' Sep 04 '22 at 02:09
  • 2
    @Fire'NLightnin' If I understand what you're saying, then I have to disagree. The counter-example is `A=[9,9,0,9,9,0,1,1,1,1,1]` with group sizes `B=[5,2,2]`. If you place the 5 group first, and maximize its profit, you'll end up with `[9,9,0,9,9], [1,1], [1,1]` for a profit of 40. But the optimal answer is `[9,9], [9,9], [1,1,1,1,1]` for a profit of 41. In short, I don't think there's any greedy algorithm that's optimal. – user3386109 Sep 05 '22 at 07:09
  • Sounds to me like we'll want to do dynamic programming. I'd probably still sort the array B in descending order so that you can exit early from an "infeasible" solution. – cadolphs Feb 03 '23 at 17:55

1 Answers1

0

You'll have to sort the groups by size: largest to smallest. The larger groups will always be better to find their maximum window sum first. For example, if you had groups of sizes [2,3] and a pricing chart of [9,9,2,1,9,9], then if you did the group of two first, your algorithm may give you [{9,9},2,1,9,9]. This would force your group of 3 to go into the less profitable spot.
Once you have your groups sorted, you're going to have to keep track of each groups' seating placement. That way, you can check to make sure you're not trying to place a group in preoccupied seats and skip ahead of those taken seats if so. Additionally, you'll want to keep track of each window's profit, so you can tally it up afterward.
I've written working code for this problem, and I needed a 3-layer nested loop, so I recommend helpful variable names (not just i, j, and k).
Edit: Additionally, like user said in the comments, make sure to eliminate any placements that do not leave enough room for the other groups.