42. 接雨水
给定 n
个非负整数表示每个宽度为 1
的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] 输出:6 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5] 输出:9
提示:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
原站题解
javascript 解法, 执行用时: 104 ms, 内存消耗: 44.6 MB, 提交时间: 2023-07-23 10:10:56
/** * @param {number[]} height * @return {number} */ var trap = function(height) { const n = height.length; if (n == 0) { return 0; } const leftMax = new Array(n).fill(0); leftMax[0] = height[0]; for (let i = 1; i < n; ++i) { leftMax[i] = Math.max(leftMax[i - 1], height[i]); } const rightMax = new Array(n).fill(0); rightMax[n - 1] = height[n - 1]; for (let i = n - 2; i >= 0; --i) { rightMax[i] = Math.max(rightMax[i + 1], height[i]); } let ans = 0; for (let i = 0; i < n; ++i) { ans += Math.min(leftMax[i], rightMax[i]) - height[i]; } return ans; };
javascript 解法, 执行用时: 64 ms, 内存消耗: 43.7 MB, 提交时间: 2023-07-23 10:10:36
/** * @param {number[]} height * @return {number} */ var trap = function(height) { let ans = 0; const stack = []; const n = height.length; for (let i = 0; i < n; ++i) { while (stack.length && height[i] > height[stack[stack.length - 1]]) { const top = stack.pop(); if (!stack.length) { break; } const left = stack[stack.length - 1]; const currWidth = i - left - 1; const currHeight = Math.min(height[left], height[i]) - height[top]; ans += currWidth * currHeight; } stack.push(i); } return ans; };
python3 解法, 执行用时: 60 ms, 内存消耗: 17.6 MB, 提交时间: 2023-07-23 10:10:20
class Solution: def trap(self, height: List[int]) -> int: ans = 0 stack = list() n = len(height) for i, h in enumerate(height): while stack and h > height[stack[-1]]: top = stack.pop() if not stack: break left = stack[-1] currWidth = i - left - 1 currHeight = min(height[left], height[i]) - height[top] ans += currWidth * currHeight stack.append(i) return ans
golang 解法, 执行用时: 12 ms, 内存消耗: 6 MB, 提交时间: 2023-07-23 10:10:03
func trap(height []int) (ans int) { stack := []int{} for i, h := range height { for len(stack) > 0 && h > height[stack[len(stack)-1]] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] if len(stack) == 0 { break } left := stack[len(stack)-1] curWidth := i - left - 1 curHeight := min(height[left], h) - height[top] ans += curWidth * curHeight } stack = append(stack, i) } return } func min(a, b int) int { if a < b { return a }; return b }
java 解法, 执行用时: 2 ms, 内存消耗: 44 MB, 提交时间: 2023-07-23 10:09:30
class Solution { public int trap(int[] height) { int ans = 0; Deque<Integer> stack = new LinkedList<Integer>(); int n = height.length; for (int i = 0; i < n; ++i) { while (!stack.isEmpty() && height[i] > height[stack.peek()]) { int top = stack.pop(); if (stack.isEmpty()) { break; } int left = stack.peek(); int currWidth = i - left - 1; int currHeight = Math.min(height[left], height[i]) - height[top]; ans += currWidth * currHeight; } stack.push(i); } return ans; } }
java 解法, 执行用时: 1 ms, 内存消耗: 43.1 MB, 提交时间: 2023-07-23 10:09:16
class Solution { public int trap(int[] height) { int n = height.length; if (n == 0) { return 0; } int[] leftMax = new int[n]; leftMax[0] = height[0]; for (int i = 1; i < n; ++i) { leftMax[i] = Math.max(leftMax[i - 1], height[i]); } int[] rightMax = new int[n]; rightMax[n - 1] = height[n - 1]; for (int i = n - 2; i >= 0; --i) { rightMax[i] = Math.max(rightMax[i + 1], height[i]); } int ans = 0; for (int i = 0; i < n; ++i) { ans += Math.min(leftMax[i], rightMax[i]) - height[i]; } return ans; } }
golang 解法, 执行用时: 12 ms, 内存消耗: 5.9 MB, 提交时间: 2023-07-23 10:08:56
func trap(height []int) (ans int) { n := len(height) if n == 0 { return } leftMax := make([]int, n) leftMax[0] = height[0] for i := 1; i < n; i++ { leftMax[i] = max(leftMax[i-1], height[i]) } rightMax := make([]int, n) rightMax[n-1] = height[n-1] for i := n - 2; i >= 0; i-- { rightMax[i] = max(rightMax[i+1], height[i]) } for i, h := range height { ans += min(leftMax[i], rightMax[i]) - h } return } func min(a, b int) int { if a < b { return a }; return b } func max(a, b int) int { if a < b { return b }; return a }
python3 解法, 执行用时: 60 ms, 内存消耗: 18.3 MB, 提交时间: 2023-07-23 10:07:55
class Solution: def trap(self, height: List[int]) -> int: if not height: return 0 n = len(height) leftMax = [height[0]] + [0] * (n - 1) for i in range(1, n): leftMax[i] = max(leftMax[i - 1], height[i]) rightMax = [0] * (n - 1) + [height[n - 1]] for i in range(n - 2, -1, -1): rightMax[i] = max(rightMax[i + 1], height[i]) ans = sum(min(leftMax[i], rightMax[i]) - height[i] for i in range(n)) return ans
python3 解法, 执行用时: 40 ms, 内存消耗: 13.8 MB, 提交时间: 2020-09-09 23:21:52
class Solution: def trap(self, height: List[int]) -> int: if len(height) < 3: return 0 max_height = max(height) max_height_index = height.index(max_height) area, tmp = 0, height[0] for i in range(max_height_index): if height[i] > tmp: tmp = height[i] else: area += tmp - height[i] n, tmp = len(height), height[-1] for i in range(n-1, max_height_index, -1): if height[i] > tmp: tmp = height[i] else: area += tmp - height[i] return area