← 返回 goldmansachs 的题目列表Trapping Rain Water
类型:online_judge
Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, calculate how much water can be trapped after it rains.
Example:
Input: heights = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation:
Each index is assumed to have a unit width, and all bars have the same width. Water can be trapped between two bars, and the maximum water height that can be trapped is determined by the smallest height of the bars on both sides. Edge cases, such as no bars on either side, should be handled appropriately.
Requirements:
Provide an efficient calculation method ensuring the algorithm’s time complexity is as low as possible.
Provide at least two additional test cases to ensure robustness of the algorithm.
Example
Input
0
1
0
2
1
0
1
3
2
1
2
1