Watch 10 video solutions for Maximum Total Area Occupied by Pistons, a hard level problem involving Array, Hash Table, String. This walkthrough by NeetCode has 427,768 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There are several pistons in an old car engine, and we want to calculate the maximum possible area under the pistons.
You are given:
height, representing the maximum height a piston can reach.positions, where positions[i] is the current position of piston i, which is equal to the current area under it.directions, where directions[i] is the current moving direction of piston i, 'U' for up, and 'D' for down.Each second:
positions[i] is incremented by 1.positions[i] == 0 or positions[i] == height, its direction will change.Return the maximum possible area under all the pistons.
Example 1:
Input: height = 5, positions = [2,5], directions = "UD"
Output: 7
Explanation:
The current position of the pistons has the maximum possible area under it.
Example 2:
Input: height = 6, positions = [0,0,6,3], directions = "UUDU"
Output: 15
Explanation:
After 3 seconds, the pistons will be in positions [3, 3, 3, 6], which has the maximum possible area under it.
Constraints:
1 <= height <= 1061 <= positions.length == directions.length <= 1050 <= positions[i] <= heightdirections[i] is either 'U' or 'D'.