# 42 Trapping Rain Water

### Problem:

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example, Given \[0,1,0,2,1,0,1,3,2,1,2,1], return 6.

![](/files/Fp6Ruv3xq7SyPQpazIAd)

The above elevation map 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. Thanks Marcos for contributing this image!

### Solutions:

```java
public class Solution {
    public int trap(int[] height) {
        int left = 0;
        int leftIndex = -1;
        int water = 0;
        int inside = 0;
        HashSet<String> range = new HashSet<String>();
        for (int i = 0; i < height.length; i ++) {
            if (height[i] >= left) {
                range.add(leftIndex+","+i);
                water = water + left * (i - leftIndex - 1) - inside;
                left = height[i];
                leftIndex = i;
                inside = 0;
                
            }
            else {
                inside += height[i];
            }
        }
        int right = 0;
        int rightIndex = height.length;
        inside = 0;
        for (int i = height.length - 1; i >= 0; i --) {
            if (height[i] >= right) {
                if (!range.contains(i+","+rightIndex)) {
                    water = water + right * (rightIndex - i - 1) - inside;
                }
                right = height[i];
                rightIndex = i;
                inside = 0;
            }
            else {
                inside += height[i];
            }
        }
        return water;
    }
}
```

```java
public class Solution {
    public int trap(int[] height) {
        if (height.length == 0) {
            return 0;
        }
        int[] left = new int[height.length];
        int[] right = new int[height.length];
        int max = height[0];
        left[0] = height[0];
        for (int i = 1; i < height.length; i ++) {
            if (height[i] > max) {
                max = height[i];
            }
            left[i] = max;
        }
        max = height[height.length - 1];
        right[height.length - 1] = height[height.length - 1];
        for (int i = height.length - 2; i >= 0; i --) {
            if (height[i] > max) {
                max = height[i];
            }
            right[i] = max;
        }
        int water = 0;
        for (int i = 0; i < height.length; i ++) {
            water +=Math.min(left[i], right[i]) - height[i];
        }
        return water;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ugopireddy.gitbook.io/leet-code-solutions/solutions_1_-_50/42-trapping-rain-water.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
