想到这两中情形的话就好理解多了。 还有Approach 4: Using 2 pointers方法,详细见:https://leetcode.com/problems/trapping-rain-water/solution/
..., left-1]intright_max =0;// the max hight in range [right+1, ..., n-1]intans =0;while(left < right){if(height[left] < height[right]){// the water trapped would be dependant on height of
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. The above elevation map is represented by array [0,1,0,2,1,0,1...
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. 双指针法 复杂度 时间O(N) 空间 O(1) 思路 我们使用两个指针,一个从左向右遍历,一个从右向左遍历。从左向右遍历时,记录下上次左边的...
LeetCode 42 Trapping Rain Water Description 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. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this...
trapped_water 赞同 6添加评论 Star C 更新于 2/16/2021, 6:04:23 AM java 從左到右掃描一遍,記錄下當下元素之前遇到的最高值。此爲當下元素的左邊界從右到左掃描一遍,記錄下當下元素之前遇到的最高值。此爲當下元素的右邊界。從頭到尾掃描一遍,水平面是左右邊界的較小值。若水平面比當下元素高,則可以加水...
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! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] ...
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 Marcosfor contributing this image! 注意对每根柱子能盛的水并不是自身能决定的,而是取决于其左右两边的柱子。
After the rain, water are trapped between the blocks. The total volume of water trapped is 4. 这道题是之前那道Trapping Rain Water的拓展,由2D变3D了,感觉很叼。但其实解法跟之前的完全不同了,之前那道题由于是二维的,我们可以用双指针来做,而这道三维的,我们需要用BFS来做,解法思路很巧妙,下面我们...
classSolution_index_stack: def trapRainWater(self, heights):stack, trapped_water = [], 0forhi_index, heightinenumerate(heights):whilestackand height >= heights[stack[-1]]: ground_height = heights[stack.pop()]ifnotstack:continuelo_index =stack[-1] water_line =min(heights[lo_index], hei...