Problem Description
You are operating a Centennial Wheel with four gondolas where each gondola can hold up to four people. Customers arrive in batches given by an array, and you can only load up to four at a time. Each rotation incurs a runningCost while each boarded customer earns a boardingCost. You want to determine the minimum number of rotations required to maximize profit. If no positive profit scenario exists, return -1.
Key Insights
- Simulate the wheel rotations while tracking waiting customers.
- At each rotation, add the new arriving customers (if any) before boarding.
- Board at most four customers per rotation and update running cost and profit.
- After processing all arrivals, continue rotations until no customers remain waiting.
- Maintain and update the maximum profit and the corresponding rotation count.
- If the computed maximum profit is not positive, return -1.
Space and Time Complexity
Time Complexity: O(n + k), where n is the number of customer arrival events and k is the number of extra rotations needed after processing the customers.
Space Complexity: O(1)
Solution
We use a simulation approach. For each rotation, first add any new arriving customers to the waiting queue, then board up to four customers. Update the total profit by considering the boarding revenue and the running cost for that rotation. Always keep track of the maximum profit seen and the rotation count at which it occurs. After processing all customer arrivals, if there are still waiting customers, continue the simulation until all customers have boarded. In the end, return the rotation count of maximum profit if profit is positive; otherwise, return -1.