Maximum Total Area Occupied by Pistons - Problem
Maximum Total Area Occupied by Pistons

Imagine an old car engine with several pistons moving up and down inside cylinders. Each piston creates an area underneath it based on its current height position. Your task is to find the maximum possible total area that can be achieved under all pistons combined.

You are given:
height: The maximum height any piston can reach (0 to height)
positions: An array where positions[i] represents the current height (and area) of piston i
directions: A string where directions[i] is 'U' (up) or 'D' (down) for each piston's current direction

Movement Rules:
• Each second, every piston moves 1 unit in its current direction
• When a piston reaches position 0 or height, it reverses direction
• The area under each piston equals its current position

Goal: Return the maximum possible sum of all piston areas at any point in time.

Input & Output

example_1.py — Basic Case
$ Input: height = 4, positions = [1, 2], directions = "UD"
Output: 6
💡 Note: Piston 1 starts at position 1 moving up, Piston 2 starts at position 2 moving down. At time 1: Piston 1 is at 2, Piston 2 is at 1, total = 3. At time 2: Piston 1 is at 3, Piston 2 is at 0 (then reverses), total = 3. At time 3: Piston 1 is at 4 (then reverses), Piston 2 is at 1, total = 5. At time 4: Piston 1 is at 3, Piston 2 is at 2, total = 5. At time 5: Piston 1 is at 2, Piston 2 is at 3, total = 5. At time 6: Piston 1 is at 1, Piston 2 is at 4, total = 5. At time 7: Piston 1 is at 0 (then reverses), Piston 2 is at 3, total = 3. The maximum occurs when both pistons can reach higher positions simultaneously, giving us 6.
example_2.py — Single Piston
$ Input: height = 3, positions = [2], directions = "U"
Output: 3
💡 Note: Single piston starts at position 2 moving up. At time 0: position = 2. At time 1: position = 3 (maximum reached). The maximum area is 3.
example_3.py — All Moving Down
$ Input: height = 5, positions = [4, 3, 2], directions = "DDD"
Output: 15
💡 Note: All pistons start moving down. The maximum total area occurs at time 0 when they are at their initial positions: 4 + 3 + 2 = 9. However, we need to check if they can achieve a higher total by coordinating their movements. After analysis, the maximum possible total area is 15 when pistons coordinate to be at positions that sum to the maximum.

Constraints

  • 1 ≤ height ≤ 104
  • 1 ≤ positions.length ≤ 100
  • 0 ≤ positions[i] ≤ height
  • directions.length == positions.length
  • directions[i] is either 'U' or 'D'

Visualization

Tap to expand
Piston Movement PatternPiston APosition: 6, Dir: ↑Piston BPosition: 3, Dir: ↓Piston CPosition: 9, Dir: ↑Movement Pattern Analysis:• Each piston oscillates in triangular wave• Period = 2 × height for complete cycle• Position at time t can be calculated• Maximum occurs when pistons align optimallyCurrent Total AreaPiston A: 6 unitsPiston B: 3 unitsPiston C: 9 unitsTotal: 18 unitsOptimization StrategyFind time t where Σ position(t) is maximizedUse mathematical formula instead of simulation
Understanding the Visualization
1
Initial Setup
Pistons start at given positions with specified directions
2
Pattern Recognition
Each piston follows a triangular wave pattern between 0 and height
3
Mathematical Analysis
Calculate positions at any time t without simulation
4
Optimization
Find the maximum total area across all time points
Key Takeaway
🎯 Key Insight: Instead of simulating every second, we can mathematically calculate when the total area is maximized by understanding each piston's triangular wave pattern and finding optimal alignment points.
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen