Input: height =5, positions =[2,5], directions ="UD"Output: 7Explanation:
The current position of the pistons has the maximum possible area under it.
Example 2:
1
2
3
4
5
Input: height =6, positions =[0,0,6,3], directions ="UUDU"Output: 15Explanation:
After 3 seconds, the pistons will be in positions `[3, 3, 3, 6]`, which has
the maximum possible area under it.
To maximize the total area under the pistons, each piston should be moved in its current direction as far as possible until it hits the boundary (either 0 or height). The final area for each piston is its maximum possible position in its direction.
classSolution {
public:int maxTotalArea(int height, vector<int>& positions, string directions) {
int n = positions.size(), ans =0;
for (int i =0; i < n; ++i) {
if (directions[i] =='U') ans += height;
else ans +=0;
}
return ans;
}
};
classSolution {
publicintmaxTotalArea(int height, int[] positions, String directions) {
int ans = 0;
for (int i = 0; i < positions.length; ++i) {
if (directions.charAt(i) =='U') ans += height;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funmaxTotalArea(height: Int, positions: IntArray, directions: String): Int {
var ans = 0for (i in positions.indices) {
if (directions[i] =='U') ans += height
}
return ans
}
}
1
2
3
classSolution:
defmaxTotalArea(self, height: int, positions: list[int], directions: str) -> int:
return sum(height if d =='U'else0for d in directions)