#1599
Medium Algorithms Maximum profit of operating a centennial wheel
Array Simulation
44.5% acceptance
Feb 25, 2026
111
254
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.
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_operations_max_profit(customers: Vec<i32>, boarding_cost: i32, running_cost: i32) -> i32 {
let mut waiting = 0i32;
let mut profit = 0i32;
let mut max_profit = 0i32;
let mut best_rot = -1i32;
let mut rot = 0i32;
let n = customers.len();
let mut i = 0;
while i < n || waiting > 0 {
if i < n {
waiting += customers[i];
i += 1;
}
let board = waiting.min(4);
waiting -= board;
profit += board * boarding_cost - running_cost;
rot += 1;
if profit > max_profit {
max_profit = profit;
best_rot = rot;
}
}
best_rot
}
}