You are the operator of a Centennial Wheel that has four gondolas , and each gondola has room for uptofour people. You have the ability to rotate the gondolas counterclockwise , which costs you runningCost
dollars.
You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed).
This means you must rotate the wheelitimes before thecustomers[i]customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again.
You can stop the wheel at any time, including beforeservingallcustomers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation.
Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return
-1.

Input: customers =[8,3], boardingCost =5, runningCost =6Output: 3Explanation: The numbers written on the gondolas are the number of people currently there.1.8 customers arrive,4 board and 4 wait for the next gondola, the wheel rotates. Current profit is4* $5 -1* $6 = $14.2.3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is8* $5 -2* $6 = $28.3. The final3 customers board the gondola, the wheel rotates. Current profit is11* $5 -3* $6 = $37.The highest profit was $37 after rotating the wheel 3 times.
Input: customers =[10,9,6], boardingCost =6, runningCost =4Output: 7Explanation:
1.10 customers arrive,4 board and 6 wait for the next gondola, the wheel rotates. Current profit is4* $6 -1* $4 = $20.2.9 customers arrive,4 board and 11wait(2 originally waiting,9 newly waiting), the wheel rotates. Current profit is8* $6 -2* $4 = $40.3. The final6 customers arrive,4 board and 13 wait, the wheel rotates. Current profit is12* $6 -3* $4 = $60.4.4 board and 9 wait, the wheel rotates. Current profit is16* $6 -4* $4 = $80.5.4 board and 5 wait, the wheel rotates. Current profit is20* $6 -5* $4 = $100.6.4 board and 1 waits, the wheel rotates. Current profit is24* $6 -6* $4 = $120.7.1 boards, the wheel rotates. Current profit is25* $6 -7* $4 = $122.The highest profit was $122 after rotating the wheel 7 times.
We simulate the process of rotating the wheel, always boarding as many customers as possible (up to 4 per rotation). We keep track of the profit after each rotation and record the rotation with the highest profit.
classSolution {
publicintminOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
int wait = 0, total = 0, ans =-1, maxProfit = 0, profit = 0, rot = 0, i = 0;
while (wait > 0 || i < customers.length) {
if (i < customers.length) wait += customers[i++];
int board = Math.min(4, wait);
wait -= board;
total += board;
rot++;
profit = total * boardingCost - rot * runningCost;
if (profit > maxProfit) {
maxProfit = profit;
ans = rot;
}
}
return maxProfit > 0 ? ans : -1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funminOperationsMaxProfit(customers: IntArray, boardingCost: Int, runningCost: Int): Int {
var wait = 0; var total = 0; var ans = -1; var maxProfit = 0; var profit = 0; var rot = 0; var i = 0while (wait > 0|| i < customers.size) {
if (i < customers.size) wait += customers[i++]
val board = minOf(4, wait)
wait -= board
total += board
rot++ profit = total * boardingCost - rot * runningCost
if (profit > maxProfit) {
maxProfit = profit
ans = rot
}
}
returnif (maxProfit > 0) ans else -1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defminOperationsMaxProfit(self, customers: list[int], boardingCost: int, runningCost: int) -> int:
wait = total = ans = rot =0 maxProfit = profit =0 i =0 ans =-1while wait >0or i < len(customers):
if i < len(customers):
wait += customers[i]
i +=1 board = min(4, wait)
wait -= board
total += board
rot +=1 profit = total * boardingCost - rot * runningCost
if profit > maxProfit:
maxProfit = profit
ans = rot
return ans if maxProfit >0else-1