Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions solution/0400-0499/0407.Trapping Rain Water II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,43 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
```

#### TypeScript

```ts
function trapRainWater(heightMap: number[][]): number {
const m = heightMap.length;
const n = heightMap[0].length;
const vis: boolean[][] = Array.from({ length: m }, () => new Array(n).fill(false));
const pq = new MinPriorityQueue<[number, number, number]>({
compare: (a, b) => a[0] - b[0],
});
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
pq.enqueue([heightMap[i][j], i, j]);
vis[i][j] = true;
}
}
}

let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
while (!pq.isEmpty()) {
const [h, i, j] = pq.dequeue();
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {
ans += Math.max(0, h - heightMap[x][y]);
vis[x][y] = true;
pq.enqueue([Math.max(h, heightMap[x][y]), x, y]);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
43 changes: 42 additions & 1 deletion solution/0400-0499/0407.Trapping Rain Water II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ The total volume of water trapped is 4.

<!-- solution:start -->

### Solution 1
### Solution 1: Priority Queue (Min Heap)

This is a variant of the trapping rain water problem. Since the heights on the matrix boundaries are fixed, we can add these boundary heights to a priority queue. Then, we repeatedly take out the minimum height from the priority queue and compare it with the heights of its four adjacent cells. If an adjacent cell's height is less than the current height, we can trap water there. The volume of trapped water is the difference between the current height and the adjacent height. We then add the larger height back to the priority queue and repeat this process until the priority queue is empty.

The time complexity is $O(m \times n \times \log (m \times n))$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -198,6 +202,43 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
```

#### TypeScript

```ts
function trapRainWater(heightMap: number[][]): number {
const m = heightMap.length;
const n = heightMap[0].length;
const vis: boolean[][] = Array.from({ length: m }, () => new Array(n).fill(false));
const pq = new MinPriorityQueue<[number, number, number]>({
compare: (a, b) => a[0] - b[0],
});
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
pq.enqueue([heightMap[i][j], i, j]);
vis[i][j] = true;
}
}
}

let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
while (!pq.isEmpty()) {
const [h, i, j] = pq.dequeue();
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {
ans += Math.max(0, h - heightMap[x][y]);
vis[x][y] = true;
pq.enqueue([Math.max(h, heightMap[x][y]), x, y]);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
32 changes: 32 additions & 0 deletions solution/0400-0499/0407.Trapping Rain Water II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function trapRainWater(heightMap: number[][]): number {
const m = heightMap.length;
const n = heightMap[0].length;
const vis: boolean[][] = Array.from({ length: m }, () => new Array(n).fill(false));
const pq = new MinPriorityQueue<[number, number, number]>({
compare: (a, b) => a[0] - b[0],
});
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
pq.enqueue([heightMap[i][j], i, j]);
vis[i][j] = true;
}
}
}

let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
while (!pq.isEmpty()) {
const [h, i, j] = pq.dequeue();
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {
ans += Math.max(0, h - heightMap[x][y]);
vis[x][y] = true;
pq.enqueue([Math.max(h, heightMap[x][y]), x, y]);
}
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can repeatedly perform modulo and division operations on $n$. Each modulo result multiplied by the current position value $p$ represents a decimal component. If the modulo result is not $0$, we add this component to our answer. Then we multiply $p$ by $10$ and continue processing the next position.

Finally, we reverse the answer to arrange it in descending order.

The time complexity is $O(\log n)$, where $n$ is the input positive integer. The space complexity is $O(\log n)$ for storing the answer.

<!-- tabs:start -->

Expand Down