A ski resort operates a chairlift whose chairs each seat at most 4 skiers. Skiers arrive in n waves: customers[i] new skiers join the lift queue immediately before the (i+1)-th chair is dispatched. Every time a chair is dispatched, up to 4 of the skiers currently waiting board it (any skiers who don't fit keep waiting for a later chair); the lift operator earns boardingCost for every skier who actually boards, and spends runningCost to run that single dispatch, regardless of how many skiers boarded on it. The operator keeps dispatching chairs, one at a time, until every skier who has arrived so far has boarded — dispatches may continue past the n given waves as long as skiers are still waiting, but the operator never dispatches once the queue is completely empty and no more skiers will ever arrive. Determine the number of dispatches at which the operator's cumulative profit (total boarding earnings minus total running costs paid so far) first reaches its highest value. If the cumulative profit is never strictly positive at any point, report that operating the lift is never worthwhile.
A single integer: the smallest number of dispatches at which the cumulative profit reaches its maximum value, if that maximum value is strictly positive; otherwise print -1.
Example 1
Input
3 10 9 6 6 4
Expected
7
Explanation
customers = [10, 9, 6], boardingCost = 6, runningCost = 4. Dispatches 1 through 6 each board exactly 4 skiers (there are always at least 4 waiting), each earning 4*6 - 4 = 20 profit, so cumulative profit after dispatch 6 is 120. A 7th dispatch is still needed because 10+9+6 = 25 skiers arrived in total and only 24 have boarded so far; it boards the last 1 skier for 1*6 - 4 = 2 profit, bringing the cumulative profit to 122 — the highest it ever reaches, since no skiers remain afterward. So the answer is 7.
Example 2
Input
2 1 1 1 10
Expected
-1
Explanation
customers = [1, 1], boardingCost = 1, runningCost = 10. Dispatch 1 boards the 1 waiting skier for profit 1*1 - 10 = -9 (cumulative -9). Dispatch 2 boards the other new skier for another 1*1 - 10 = -9 (cumulative -18). The cumulative profit is negative at every dispatch and never exceeds its starting value of 0, so operating the lift is never worthwhile and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →