042 Trapping Rain Water

这道题有一个直观的想法, 就是分别记录每个点的左侧和右侧最大值 对于height[i] 这一点能装的水等于 min(leftMax[i], rightMax[i]) - height[i]. 这个解法需要扫描2次序列。

以下的方法只需要扫描一次序列即可。

class Solution:
    # @param {integer[]} height
    # @return {integer}
    def trap(self, height):
        stack = []
        ans = 0
        i, length = 0, len(height)
        while i < length:
            if stack == []:
                stack.append([height[i], 1])
            else:
                if height[i] < stack[-1][0]:
                    stack.append([height[i], 1])
                else:
                    if height[i] >= stack[0][0]:
                        hh = stack[0][0]
                        while stack != []:
                            col = stack.pop()
                            h = col[0]
                            l = col[1]
                            ans += (hh - h) * l
                        stack.append([height[i], 1])
                    else:
                        tmp = 1
                        hh = height[i]
                        while hh >= stack[-1][0]:
                            col = stack.pop()
                            h = col[0]
                            l = col[1]
                            ans += (hh - h) * l
                            tmp += l
                        stack.append([hh, tmp])
            i += 1
        return ans
时间: 2025-01-04 06:49:38

042 Trapping Rain Water的相关文章

LeetCode 042 Trapping Rain Water

题目要求:Trapping Rain Water 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 elevatio

【LeetCode】042 Trapping Rain Water

题目: 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

Java for LeetCode 042 Trapping Rain Water

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. 解题思路: 先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,J

[leetcode][042] Trapping Rain Water (Java)

题目在这里: https://leetcode.com/problems/trapping-rain-water/ [标签] Array; Stack; Two Pointers 这个题我感觉很难,我自己是完全不会.下面贴的是别人想到的好方法,自己适当做了些简单调整. 我觉得,这个解法非常巧妙的使用了双向的 two pointers.用到的变量有左右两个指针,一个在最左边,一个在最右边,然后两个一起往中间走.在走的时候维护两个变量,left barrier 和 right barrier,即左边

有意思的数学题:Trapping Rain Water

LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分 假设:积木宽度均为1 输入:各个积木的高度 输出:所有积木能容纳水的“面积” 思考过程 1. 逐一求积木的间隔似乎不太容易.特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度. 2. 既然如此,可否先求出3-7

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water 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

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

Trapping Rain water 诸多细节须注意

Trapping Rain Water Total Accepted: 35650 Total Submissions: 118319My Submissions Question Solution 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.