diff --git a/solution/0400-0499/0407.Trapping Rain Water II/README.md b/solution/0400-0499/0407.Trapping Rain Water II/README.md index 33514ac2eecc6..adc4d8d00f50b 100644 --- a/solution/0400-0499/0407.Trapping Rain Water II/README.md +++ b/solution/0400-0499/0407.Trapping Rain Water II/README.md @@ -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; +} +``` + diff --git a/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md b/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md index c6635c7681d44..89e2a547e9298 100644 --- a/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md +++ b/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md @@ -55,7 +55,11 @@ The total volume of water trapped is 4. -### 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. @@ -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; +} +``` + diff --git a/solution/0400-0499/0407.Trapping Rain Water II/Solution.ts b/solution/0400-0499/0407.Trapping Rain Water II/Solution.ts new file mode 100644 index 0000000000000..8b89899a35851 --- /dev/null +++ b/solution/0400-0499/0407.Trapping Rain Water II/Solution.ts @@ -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; +} diff --git a/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md b/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md index 2e62f213a7950..6ab3c6f9c6db5 100644 --- a/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md +++ b/solution/3600-3699/3697.Compute Decimal Representation/README_EN.md @@ -72,7 +72,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Co -### 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.