# [LeetCode][42. Trapping Rain Water] From Brute Force to DP to Two Pointers with Detail Explaination

**By Long Luo**

This article is the solution From Brute Force to DP to Two Pointers with Detail Explaination of Problem 42. Trapping Rain Water.

# Intuition

It’s like Leetcode 11. Container With Most Water , we can consider the black block as a wall, the blue block as water. Given an array, each element represents the height of the wall from left to right, find out how many units of water can be held.

The solution can be refered 2 Approaches: BF and Two Pointers with Image Explaination .

# 1. Brute Force

It’s easy to use the brute force approach. The time complexity is $O(max^n)$, so it will **TLE**.

1 | // Row time: O(max * n) space: O(1) |

## Analysis

**Time Complexity**: $O(max^n)$**Space Complexity**: $O(1)$

# 2. Calculate by Column

To find the water in each column, we only need to focus on:

- Current column;
- The highest wall on the left;
- The highest wall on the right.

To find how much water filled, we only need to look at the shorter one of the tallest wall on the left and the tallest wall on the right.

Therefore, according to the height of the shorter wall and the wall of the current column, it can be divided into three cases.

- $min(leftMax, rightMax) > current$:

Imagine pouring water between the tallest walls on either side. How much water will the column being asked for?

Obviously, the shorter side, which is the height of the wall on the left, minus the height of the current column, which is $2 - 1 = 1$, can store one unit of water.

- $min(leftMax, rightMax) < current$:

Imagine pouring water between the tallest walls on either side. How much water will the column being asked for?

The column being sought will not have water because it is larger than the lower walls on either side.

- $min(leftMax, rightMax) == current$:

As in the previous case, there will be no water.

1 | // Col time: O(n^2) space: O(1) |

## Analysis

**Time Complexity**: $O(n^2)$**Space Complexity**: $O(1)$

# 3. Dynamic Programming

In Approache 2, for each column, we have to traverse all the heights to get the tallest wall on the left and the tallest wall on the right, we can optimize it.

We can use **extra space** to tradeoff the time complexity.

Use two arrays, $max_left[i]$ and $max_right[i]$, which represents the height of the tallest wall on the left or right of column $i$.

$max_left[i] = Max(max_left[i-1], height[i-1])$. Choose the higher height on the left side of the wall in front of it and the height of the wall in front of it, which is the highest wall on the left side of the current column.

$max_right[i] = Max(max_right[i+1],height[i+1])$. The highest height on the right side of the wall behind it and the height of the wall behind it choose a larger one, which is the highest wall on the right side of the current column.

1 | // DP time: O(n) space: O(n) |

## Analysis

**Time Complexity**: $O(n)$**Space Complexity**: $O(n)$

# 4. Two Pointers

In DP Approach, we can often optimize the space complexity.

We can see that the elements in the $max_left[i]$ and $max_right[i]$ arrays are actually only used **once**, and then never used again. So we can use just use one element instead of an array.

1 | // Two Pointers time: O(n) space: O(1) |

## Analysis

**Time Complexity**: $O(n)$**Space Complexity**: $O(1)$

All suggestions are welcome.

If you have any query or suggestion please comment below.

Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗