Tap here to switch tabs
Problem
Submissions

Problem: Maximum Total Area Occupied by Pistons

Try to solve the Maximum Total Area Occupied by Pistons problem.

Statement

You are given an integer height, an integer array positions, and a string directions representing the initial states of several pistons moving inside vertical chambers.

Each piston at index i has an initial vertical position positions[i] and an initial moving direction given by directions[i]:

  • 'U' indicates that the piston is moving upward.

  • 'D' indicates that the piston is moving downward.

Every second, each piston changes its position by 11 unit according to its current direction:

  • If a piston is at height and moving upward, it reverses its direction to downward.

  • If a piston is at 00 and moving downward, it reverses its direction to upward.

  • Otherwise, it continues moving in its current direction.

The total area occupied at any time is the sum of the positions of all pistons.

Return the maximum total area occupied by the pistons at any point in time.

Constraints:

  • 1≤1 \leq height ≤106\leq 10^6

  • 1≤1 \leq positions.length ==== directions.length ≤105\leq 10^5

  • 0≤0 \leq positions[i] ≤\leq height

  • directions[i] is either 'U' or 'D'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Total Area Occupied by Pistons

Try to solve the Maximum Total Area Occupied by Pistons problem.

Statement

You are given an integer height, an integer array positions, and a string directions representing the initial states of several pistons moving inside vertical chambers.

Each piston at index i has an initial vertical position positions[i] and an initial moving direction given by directions[i]:

  • 'U' indicates that the piston is moving upward.

  • 'D' indicates that the piston is moving downward.

Every second, each piston changes its position by 11 unit according to its current direction:

  • If a piston is at height and moving upward, it reverses its direction to downward.

  • If a piston is at 00 and moving downward, it reverses its direction to upward.

  • Otherwise, it continues moving in its current direction.

The total area occupied at any time is the sum of the positions of all pistons.

Return the maximum total area occupied by the pistons at any point in time.

Constraints:

  • 1≤1 \leq height ≤106\leq 10^6

  • 1≤1 \leq positions.length ==== directions.length ≤105\leq 10^5

  • 0≤0 \leq positions[i] ≤\leq height

  • directions[i] is either 'U' or 'D'.