Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5] Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to calculate trapped rainwater involves examining each location individually. For each location, we'll determine how much water it can hold by looking at the highest boundaries around it. After assessing each location, we sum the water from all the locations to get the total trapped rainwater.
Here's how the algorithm would work step-by-step:
def trapping_rain_water_brute_force(heights):
total_water_trapped = 0
number_of_locations = len(heights)
for current_location in range(number_of_locations):
max_height_left = 0
# Find the tallest wall to the left of the current location
for left_location in range(current_location):
max_height_left = max(max_height_left, heights[left_location])
max_height_right = 0
# Find the tallest wall to the right of the current location
for right_location in range(current_location + 1, number_of_locations):
max_height_right = max(max_height_right, heights[right_location])
# The shorter wall determines the water level
shorter_wall = min(max_height_left, max_height_right)
# Calculate trapped water if possible
if heights[current_location] < shorter_wall:
total_water_trapped += shorter_wall - heights[current_location]
return total_water_trapped
The core idea is to calculate how much water can be trapped at each position. This is done by figuring out the highest bar to the left and the highest bar to the right of each position, then using the lower of these two heights to determine the water level that can be held.
Here's how the algorithm would work step-by-step:
def trapping_rain_water(heights):
number_of_bars = len(heights)
if number_of_bars == 0:
return 0
max_height_left = [0] * number_of_bars
max_height_right = [0] * number_of_bars
# Calculate max height to the left for each position
max_height_left[0] = heights[0]
for i in range(1, number_of_bars):
max_height_left[i] = max(heights[i], max_height_left[i - 1])
# Calculate max height to the right for each position
max_height_right[number_of_bars - 1] = heights[number_of_bars - 1]
for i in range(number_of_bars - 2, -1, -1):
max_height_right[i] = max(heights[i], max_height_right[i + 1])
total_water_trapped = 0
for i in range(number_of_bars):
# Water level is dictated by the shorter of the max heights
water_level = min(max_height_left[i], max_height_right[i])
# Only trap water if water level is higher than current height
if water_level > heights[i]:
total_water_trapped += water_level - heights[i]
return total_water_trapped
Case | How to Handle |
---|---|
Empty or null input array | Return 0 immediately, as no water can be trapped. |
Array with only one or two elements | Return 0 immediately, as no water can be trapped between bars with fewer than 3 columns. |
Array with all elements equal to 0 | Return 0, as there are no bars to trap any water. |
Array with all elements having the same non-zero value | Return 0, as there is no variation in height to trap water. |
Array with only one peak element (all other elements are lower) | The algorithm should correctly calculate water trapped on either side of the peak. |
Monotonically increasing or decreasing array | Return 0, as there are no dips to hold water. |
Large height values leading to potential integer overflow | Use a data type (e.g., long) that can accommodate larger values to prevent overflow during calculations. |
Array with very large size impacting performance | The solution should have a time complexity of O(n) to scale efficiently with larger inputs; space complexity considerations are also important. |