Problem#
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 wheeli
times 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 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
.
Examples#
Example 1#
1
2
3
4
5
6
7
8
9
10
|

Input: customers = [8,3], boardingCost = 5, runningCost = 6
Output: 3
Explanation: 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 is 4 * $5 - 1 * $6 = $14.
2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28.
3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37.
The highest profit was $37 after rotating the wheel 3 times.
|
Example 2#
1
2
3
4
5
6
7
8
9
10
11
|
Input: customers = [10,9,6], boardingCost = 6, runningCost = 4
Output: 7
Explanation:
1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20.
2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40.
3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60.
4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80.
5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100.
6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120.
7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122.
The highest profit was $122 after rotating the wheel 7 times.
|
Example 3#
1
2
3
4
5
6
7
8
9
|
Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
Output: -1
Explanation:
1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89.
2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177.
3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269.
4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357.
5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447.
The profit was never positive, so return -1.
|
Constraints#
n == customers.length
1 <= n <= 10^5
0 <= customers[i] <= 50
1 <= boardingCost, runningCost <= 100
Solution#
Method 1 – Simulation with Greedy Boarding#
Intuition#
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.
Approach#
- Initialize variables for waiting customers, total boarded, current profit, max profit, and the answer (rotation count).
- For each rotation (while there are customers to process or waiting customers):
- Add new arrivals to the waiting queue.
- Board up to 4 customers.
- Update total boarded and waiting.
- Calculate profit: total boarded * boardingCost - rotations * runningCost.
- If profit exceeds max profit, update max profit and record the rotation.
- Return the rotation count with max profit, or -1 if profit never positive.
Code#
C++#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Solution {
public:
int minOperationsMaxProfit(vector<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.size()) {
if (i < customers.size()) wait += customers[i++];
int board = 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
19
20
21
22
23
24
25
|
func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int) int {
wait, total, ans, maxProfit, profit, rot, i := 0, 0, -1, 0, 0, 0, 0
for wait > 0 || i < len(customers) {
if i < len(customers) {
wait += customers[i]
i++
}
board := 4
if wait < 4 {
board = wait
}
wait -= board
total += board
rot++
profit = total*boardingCost - rot*runningCost
if profit > maxProfit {
maxProfit = profit
ans = rot
}
}
if maxProfit > 0 {
return ans
}
return -1
}
|
Java#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Solution {
public int minOperationsMaxProfit(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;
}
}
|
Kotlin#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Solution {
fun minOperationsMaxProfit(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 = 0
while (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
}
}
return if (maxProfit > 0) ans else -1
}
}
|
Python#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Solution:
def minOperationsMaxProfit(self, customers: list[int], boardingCost: int, runningCost: int) -> int:
wait = total = ans = rot = 0
maxProfit = profit = 0
i = 0
ans = -1
while wait > 0 or 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 > 0 else -1
|
Rust#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
impl Solution {
pub fn min_operations_max_profit(customers: Vec<i32>, boarding_cost: i32, running_cost: i32) -> i32 {
let (mut wait, mut total, mut ans, mut max_profit, mut profit, mut rot, mut i) = (0, 0, -1, 0, 0, 0, 0);
while wait > 0 || i < customers.len() {
if i < customers.len() {
wait += customers[i];
i += 1;
}
let board = wait.min(4);
wait -= board;
total += board;
rot += 1;
profit = total * boarding_cost - rot * running_cost;
if profit > max_profit {
max_profit = profit;
ans = rot as i32;
}
}
if max_profit > 0 { ans } else { -1 }
}
}
|
TypeScript#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Solution {
minOperationsMaxProfit(customers: number[], boardingCost: number, runningCost: number): number {
let 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++];
const 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;
}
}
|
Complexity#
- ⏰ Time complexity:
O(n)
, where n is the number of customer arrivals. Each rotation is processed once.
- 🧺 Space complexity:
O(1)
, only a constant number of variables are used.