Maximum Total Area Occupied by Pistons - Problem

There are several pistons in an old car engine, and we want to calculate the maximum possible area under the pistons.

You are given:

  • An integer height, representing the maximum height a piston can reach.
  • An integer array positions, where positions[i] is the current position of piston i, which is equal to the current area under it.
  • A string directions, where directions[i] is the current moving direction of piston i, 'U' for up, and 'D' for down.

Each second:

  • Every piston moves in its current direction 1 unit. e.g., if the direction is up, positions[i] is incremented by 1.
  • If a piston has reached one of the ends, i.e., positions[i] == 0 or positions[i] == height, its direction will change.

Return the maximum possible area under all the pistons.

Input & Output

Example 1 — Basic Case
$ Input: height = 4, positions = [1,3,2], directions = "UDU"
Output: 9
💡 Note: At time 3: piston 1 reaches position 4 (up from 1), piston 2 reaches position 2 (down from 3, hit 0, bounced up), piston 3 reaches position 3 (up from 2, hit 4, bounced down). Total area = 4+2+3 = 9
Example 2 — All Moving Up
$ Input: height = 3, positions = [1,2], directions = "UU"
Output: 5
💡 Note: Both pistons move up. At time 1: positions become [2,3], total = 5. At time 2: piston 2 bounces and becomes [3,2], total = 5. Maximum area is 5
Example 3 — Single Piston
$ Input: height = 5, positions = [2], directions = "D"
Output: 5
💡 Note: Piston moves down to 0, then up to maximum height 5. Maximum area is 5 when it reaches the top

Constraints

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

Visualization

Tap to expand
Maximum Total Area Occupied by Pistons INPUT height = 4 (max height) 4 3 2 1 0 P0 pos=1 U P1 pos=3 D P2 pos=2 U height = 4 positions = [1, 3, 2] directions = "UDU" Initial area: 1+3+2 = 6 ALGORITHM STEPS 1 Calculate Events Find when each piston hits top (height) or bottom (0) 2 Track Area Change U piston: +1 per second D piston: -1 per second 3 Process Events At each reversal point, update net direction 4 Find Maximum Track max area reached during simulation Area Over Time MAX=8 t=0 t=2 t FINAL RESULT At time t=2 (maximum area) 4 3 2 1 0 P0=3 P1=1 P2=4 Area Calculation at t=2: 3 + 1 + 4 = 8 (sum of all positions) OUTPUT 8 Key Insight: The total area changes by (U_count - D_count) each second. Net change = +1 (2 up, 1 down). Track events when pistons hit boundaries and reverse direction. Maximum occurs when net direction changes from positive to negative. Use event-driven simulation for O(n log n) solution. TutorialsPoint - Maximum Total Area Occupied by Pistons | Mathematical Analysis Approach
Asked in
Google 25 Amazon 18 Microsoft 15
28.4K Views
Medium Frequency
~35 min Avg. Time
892 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