Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
Suppose there is a Ferris wheel with four cabins and each cabin can contain four passengers. The wheel rotates counter-clockwise, and for each rotation, it costs run amount of money. We have an array cust that contains n items where each item i signifies the number of people waiting to get into the Ferris wheel before the i-th rotation. To board the wheel, each customer pays an amount board for one anti-clockwise rotation. The people waiting in line should not wait if there is any vacant seat available in any cabin.
So, if the input is like cust = [6,4], board = 6, run = 4, then the output will be 3 rotations.
Problem Analysis
Let's trace through the example ?
- Initially: 6 people are waiting. 4 people board the first cabin, 2 remain waiting.
- After 1st rotation: 4 more people arrive (total waiting = 2 + 4 = 6). 4 people board the second cabin, 2 remain.
- After 2nd rotation: All remaining 2 people board the third cabin.
- Result: 3 rotations needed. Profit = (10 customers × 6) - (3 rotations × 4) = 48.
Algorithm Steps
To solve this, we follow these steps ?
- Track waiting customers and profit for each rotation
- Find the rotation that gives maximum profit
- Handle remaining customers after processing the input array
- Return the minimum rotations needed for maximum profit
Solution
def solve(cust, board, run):
res = -1
max_profit = 0
current_profit = 0
waiting = 0
# Process each group of arriving customers
for idx, val in enumerate(cust):
waiting += val
boarded = min(4, waiting) # Maximum 4 can board per rotation
waiting -= boarded
current_profit += boarded * board - run
# Track the rotation with maximum profit
if max_profit < current_profit:
res = idx + 1
max_profit = current_profit
# Handle remaining waiting customers
full_rotations = waiting // 4
remaining_customers = waiting % 4
# Add full rotations if profitable
if 4 * board > run:
res += full_rotations
# Add one more rotation for remaining customers if profitable
if remaining_customers * board > run:
res += 1
return res
# Test the solution
result = solve([6, 4], 6, 4)
print(f"Minimum rotations needed: {result}")
Minimum rotations needed: 3
Step-by-Step Execution
def solve_with_trace(cust, board, run):
res = -1
max_profit = 0
current_profit = 0
waiting = 0
print(f"Board cost: {board}, Run cost: {run}")
print("=" * 50)
for idx, val in enumerate(cust):
waiting += val
boarded = min(4, waiting)
waiting -= boarded
current_profit += boarded * board - run
print(f"Rotation {idx + 1}:")
print(f" New arrivals: {val}")
print(f" Customers boarded: {boarded}")
print(f" Still waiting: {waiting}")
print(f" Current profit: {current_profit}")
if max_profit < current_profit:
res = idx + 1
max_profit = current_profit
print(f" ? Best rotation so far: {res}")
print()
# Handle remaining customers
full_rotations = waiting // 4
remaining = waiting % 4
print(f"Remaining customers: {waiting}")
print(f"Full rotations needed: {full_rotations}")
print(f"Remaining after full rotations: {remaining}")
if 4 * board > run:
res += full_rotations
print(f"Added {full_rotations} profitable full rotations")
if remaining * board > run:
res += 1
print(f"Added 1 rotation for {remaining} remaining customers")
return res
# Trace the example
result = solve_with_trace([6, 4], 6, 4)
print(f"\nFinal answer: {result} rotations")
Board cost: 6, Run cost: 4 ================================================== Rotation 1: New arrivals: 6 Customers boarded: 4 Still waiting: 2 Current profit: 20 ? Best rotation so far: 1 Rotation 2: New arrivals: 4 Customers boarded: 4 Still waiting: 2 Current profit: 40 ? Best rotation so far: 2 Remaining customers: 2 Full rotations needed: 0 Remaining after full rotations: 2 Added 1 rotation for 2 remaining customers Final answer: 3 rotations
Key Points
- Greedy approach: Process customers as they arrive and track profit
- Capacity constraint: Maximum 4 customers per rotation
- Profit calculation: (customers × board_cost) - run_cost
- Optimal stopping: Continue only if additional rotations are profitable
Conclusion
This algorithm efficiently finds the minimum rotations needed to maximize Ferris wheel profit by processing customer arrivals sequentially and handling remaining customers optimally. The time complexity is O(n) where n is the length of the customer array.
