Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1599. Maximum Profit of Operating a Centennial Wheel - Leetcode Solution

Problem Description

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.
Your task is to determine the minimum number of rounds needed to achieve the maximum profit. If it is impossible to make any profit, return -1.

Key constraints:

  • At most 4 customers can board per round.
  • All waiting customers must wait for the next round if they can't board immediately.
  • Profit is calculated as: total_boarded_customers * boardingCost - total_rounds * runningCost.
  • You cannot skip rounds; you must run the wheel as long as there are waiting or arriving customers.

Thought Process

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 number of waiting customers
  • The total number of customers who have boarded
  • The current profit after each round
After each round, we update the profit and check if it's the highest seen so far. We also record the round number when this maximum profit occurs.

The process continues until all customers have boarded (i.e., no more arrivals and no more waiting customers).

Solution Approach

We solve the problem using a simulation approach:

  1. Initialize variables:
    • waiting: Number of customers waiting to board (initially 0)
    • i: Index for the customers array
    • rounds: Number of rounds run so far
    • total_boarded: Total customers who have boarded
    • max_profit: Highest profit achieved so far
    • best_round: Round number with the highest profit
  2. Simulate rounds:
    • While there are still waiting customers or unprocessed arrivals, do:
      • Add newly arrived customers (if any) to waiting
      • Determine to_board = min(4, waiting)
      • Update waiting and total_boarded
      • Increment rounds
      • Calculate current profit: total_boarded * boardingCost - rounds * runningCost
      • If current profit is higher than max_profit, update max_profit and best_round
  3. Return result:
    • If max_profit > 0, return best_round
    • Otherwise, return -1

This approach ensures we process all customers in order and track profit optimally.

Example Walkthrough

Given:

  • customers = [8, 3]
  • boardingCost = 5
  • runningCost = 6
Simulation:
  1. Round 1: 8 arrive, waiting=8. Board 4, waiting=4, total_boarded=4, profit=4*5-1*6=14.
  2. Round 2: 3 arrive, waiting=7. Board 4, waiting=3, total_boarded=8, profit=8*5-2*6=28.
  3. Round 3: 0 arrive, waiting=3. Board 3, waiting=0, total_boarded=11, profit=11*5-3*6=37.
  4. Round 4: 0 arrive, waiting=0. No more customers, end.

The highest profit is 37 at round 3. So, the answer is 3.

Time and Space Complexity

Brute-force: Would require checking all possible boardings per round, which is exponential and infeasible.

Optimized simulation:

  • Time Complexity: O(N + W), where N is the length of 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.
  • Space Complexity: O(1), since we only use a few variables for bookkeeping.

Summary

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.

Code Implementation

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;
};