[LeetCode] Maximum Profit of Operating a Centennial Wheel

1599. Maximum Profit of Operating a Centennial Wheel

You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four 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 wheel i times before the customers[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 before serving all customers. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int profit = 0, ma = 0, res = -1, wait = 0, rotate = 1;
for(auto& c : customers) {
wait += c;
int board = min(wait,4);
profit += board*boardingCost - runningCost;
wait -= board;
if(profit > ma) {
ma = profit;
res = rotate;
}
rotate++;
}
while(wait) {
int board = min(wait,4);
profit += board*boardingCost - runningCost;
wait -= board;
if(profit > ma) {
ma = profit;
res = rotate;
}
rotate++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/17/PS/LeetCode/maximum-profit-of-operating-a-centennial-wheel/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.