The "Maximum Profit of Operating a Centennial Wheel" problem presents a scenario where you manage a carnival wheel ride. The wheel has four seats and operates in rounds. Each round, you can board up to four waiting customers, and each customer pays a boarding cost. However, each round you run the wheel, regardless of how many customers are boarded, you incur a running cost.
You are given:
customers
: An array where customers[i]
is the number of customers that arrive just before the i
-th round.boardingCost
: The profit you make for each customer who boards.runningCost
: The cost incurred for each round the wheel runs.-1
.
Key constraints:
total_boarded_customers * boardingCost - total_rounds * runningCost
.The first instinct might be to simulate every possible combination of customers boarding in each round to maximize profit. However, due to the constraints (max 4 per round, must process all arrivals in order), a brute-force approach would be inefficient.
Instead, we recognize that since only 4 can board per round, and customers arrive in a queue, we can simulate the process round by round, keeping track of:
The process continues until all customers have boarded (i.e., no more arrivals and no more waiting customers).
We solve the problem using a simulation approach:
waiting
: Number of customers waiting to board (initially 0)i
: Index for the customers
arrayrounds
: Number of rounds run so fartotal_boarded
: Total customers who have boardedmax_profit
: Highest profit achieved so farbest_round
: Round number with the highest profitwaiting
to_board = min(4, waiting)
waiting
and total_boarded
rounds
total_boarded * boardingCost - rounds * runningCost
max_profit
, update max_profit
and best_round
max_profit > 0
, return best_round
-1
This approach ensures we process all customers in order and track profit optimally.
Given:
customers = [8, 3]
boardingCost = 5
runningCost = 6
The highest profit is 37 at round 3. So, the answer is 3
.
Brute-force: Would require checking all possible boardings per round, which is exponential and infeasible.
Optimized simulation:
customers
and W is the number of extra rounds needed to board all waiting customers after arrivals finish. In practice, this is linear with respect to the total number of customers.The solution efficiently simulates the operation of the Centennial Wheel by processing customers in rounds, always boarding up to 4 per round, and calculating profit after each round. By tracking the round with the maximum profit, we find the optimal stopping point. The approach is straightforward, leverages only constant extra space, and avoids unnecessary computation, making it both elegant and efficient.
class Solution:
def minOperationsMaxProfit(self, customers, boardingCost, runningCost):
waiting = 0
total_boarded = 0
rounds = 0
max_profit = 0
best_round = -1
i = 0
n = len(customers)
while waiting > 0 or i < n:
if i < n:
waiting += customers[i]
to_board = min(4, waiting)
waiting -= to_board
total_boarded += to_board
rounds += 1
profit = total_boarded * boardingCost - rounds * runningCost
if profit > max_profit:
max_profit = profit
best_round = rounds
i += 1
return best_round if max_profit > 0 else -1
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int waiting = 0, total_boarded = 0, rounds = 0;
int max_profit = 0, best_round = -1;
int i = 0, n = customers.size();
while (waiting > 0 || i < n) {
if (i < n) waiting += customers[i];
int to_board = min(4, waiting);
waiting -= to_board;
total_boarded += to_board;
rounds++;
int profit = total_boarded * boardingCost - rounds * runningCost;
if (profit > max_profit) {
max_profit = profit;
best_round = rounds;
}
i++;
}
return max_profit > 0 ? best_round : -1;
}
};
class Solution {
public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
int waiting = 0, totalBoarded = 0, rounds = 0;
int maxProfit = 0, bestRound = -1;
int i = 0, n = customers.length;
while (waiting > 0 || i < n) {
if (i < n) waiting += customers[i];
int toBoard = Math.min(4, waiting);
waiting -= toBoard;
totalBoarded += toBoard;
rounds++;
int profit = totalBoarded * boardingCost - rounds * runningCost;
if (profit > maxProfit) {
maxProfit = profit;
bestRound = rounds;
}
i++;
}
return maxProfit > 0 ? bestRound : -1;
}
}
var minOperationsMaxProfit = function(customers, boardingCost, runningCost) {
let waiting = 0, totalBoarded = 0, rounds = 0;
let maxProfit = 0, bestRound = -1;
let i = 0, n = customers.length;
while (waiting > 0 || i < n) {
if (i < n) waiting += customers[i];
let toBoard = Math.min(4, waiting);
waiting -= toBoard;
totalBoarded += toBoard;
rounds++;
let profit = totalBoarded * boardingCost - rounds * runningCost;
if (profit > maxProfit) {
maxProfit = profit;
bestRound = rounds;
}
i++;
}
return maxProfit > 0 ? bestRound : -1;
};