Buffet Day Gains
Problem statement
A buffet has nbSeats seats. Guest IDs are zero-based, and payingGuests[g] is the amount guest g pays the first time they complete a visit while seated.
The array guestMovements lists guest appearances in chronological order. Each guest's first appearance is an arrival, the next is a departure, and later appearances continue alternating.
- An arriving guest takes a free seat. If every seat is occupied, the guest joins the end of a FIFO waiting line.
- A departing guest who is waiting leaves the line without paying.
- A departing guest who is seated frees the seat and pays if they have never paid before. The earliest waiting guest then takes the free seat, if one exists.
- A guest may visit more than once but pays at most once during the day.
Return the total gains for the day as a 64-bit integer.
Function
computeDayGains(nbSeats: int, payingGuests: int[], guestMovements: int[]) → longExamples
Example 1
nbSeats = 1payingGuests = [10, 20]guestMovements = [0, 1, 0, 1]return = 30Guest 0 sits first while guest 1 waits. Guest 0 departs and pays 10, so guest 1 takes the seat. Guest 1 later departs and pays 20.
Example 2
nbSeats = 1payingGuests = [5, 7]guestMovements = [0, 1, 1, 0, 0, 0]return = 5Guest 1 leaves the waiting line and never pays. Guest 0 pays 5 on the first seated departure, then revisits without paying again.
Example 3
nbSeats = 0payingGuests = [9]guestMovements = [0, 0]return = 0With no seats, guest 0 waits and then leaves without paying.
Constraints
1 ≤ payingGuests.length ≤ 100000.0 ≤ nbSeats ≤ payingGuests.length.0 ≤ payingGuests[i] ≤ 10^9.0 ≤ guestMovements.length ≤ 200000.- Every movement is a valid guest ID, and each guest's appearances alternate between arrival and departure.
- No guest appears again before leaving their current seated or waiting state.
- The answer fits in a signed 64-bit integer.