# Trapping Rain Water II

{% embed url="<https://www.lintcode.com/problem/364/>" %}

## Solution 1 (Java)

```java
public class Solution {

    private class Cell {
        int x, y, h;
        public Cell(int x, int y, int h) {
            this.x = x;
            this.y = y;
            this.h = h;
        }
    }

    /**
     * @param heights: a matrix of integers
     * @return: an integer
     */
    public int trapRainWater(int[][] heights) {
        if (heights == null || heights.length == 0)
            return 0;
        PriorityQueue<Cell> minHeap = new PriorityQueue<>(new Comparator<Cell>() {
            public int compare(Cell a, Cell b) {
                return a.h - b.h;
            }
        });
        int n = heights.length, m = heights[0].length; // n = 5, m = 4
        boolean[][] visited = new boolean[n][m];

        for (int i = 0; i < n; i++) {
            minHeap.offer(new Cell(i, 0, heights[i][0]));
            minHeap.offer(new Cell(i, m - 1, heights[i][m - 1]));
            visited[i][0] = true;
            visited[i][m - 1] = true;
        }
        for (int i = 0; i < m; i++) {
            minHeap.offer(new Cell(0, i, heights[0][i]));
            minHeap.offer(new Cell(n - 1, i, heights[n - 1][i]));
            visited[0][i] = true;
            visited[n - 1][i] = true;
        }

        int water = 0;
        int[] dx = {1, 0, -1, 0};
        int[] dy = {0, 1, 0, -1};
        while (!minHeap.isEmpty()) {
            Cell curr = minHeap.poll();
            for (int i = 0; i < 4; i++) {
                int nx = curr.x + dx[i];
                int ny = curr.y + dy[i];
                if (0 <= nx && nx < n && 0 <= ny && ny < m && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    if (curr.h > heights[nx][ny]) {
                        water += curr.h - heights[nx][ny];
                        minHeap.offer(new Cell(nx, ny, curr.h));
                    } else {
                        minHeap.offer(new Cell(nx, ny, heights[nx][ny]));
                    }
                    // minHeap.offer(new Cell(nx, ny, Math.max(curr.h, heights[nx][ny])));
                    // water = water + Math.max(0, curr.h - heights[nx][ny]);
                }
            }
        }
        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://blog.yushunchen.com/algo/heap-and-priority-queue/trapping-rain-water-ii.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.
