[LeetCode]42. 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.

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]
Output: 6

难度

Hard

方法

  1. 找到容器最高点位置max_height_index, 最高点高度max_height。找到最高点后,左边和右边的bar都能跟其形成容器蓄水
  2. 从左往右处理至max_height_index, left_max记录遍历时左边区间的最高高度, 如果height[i] > left_max, 则该格无法蓄水,将left_max更新为height[i]; 如果height[i] < left_max,则该格可以蓄水left_max - height[i]
  3. 从右往左处理至max_height_index,right_max记录遍历时右边区间的最高高度,如果height[i] > right_max, 则该格无法蓄水,right_max更新为height[i]; 如果height[i] < right_max,则该格可以蓄水left_max - height[i]
  4. 将每格可以蓄的水累加即可

python代码

# -*- coding:utf-8 -*-

class Solution(object):
    def trap(self, height):
        """
        1. 找到容器最高点位置max_height_index, 最高点高度max_height。找到最高点后,左边和右边的bar都能跟其形成容器蓄水
        2. 从左往右处理至max_height_index, left_max记录遍历时左边区间的最高高度, 如果height[i] > left_max, 则该格无法蓄水,
           将left_max更新为height[i]; 如果height[i] < left_max,则该格可以蓄水left_max - height[i]
        3. 从右往左处理至max_height_index,right_max记录遍历时右边区间的最高高度,如果height[i] > right_max, 则该格无法蓄水,
           将right_max更新为height[i]; 如果height[i] < right_max,则该格可以蓄水left_max - height[i]
        4. 将每格可以蓄的水累加即可
        :param height: List[int]
        :return: int
        """
        result = 0
        max_height = 0
        max_height_index = 0
        for i in xrange(len(height)):
            if height[i] > max_height:
                max_height = height[i]
                max_height_index = i

        left_max = 0
        for i in xrange(0, max_height_index):
            if height[i] > left_max:
                left_max = height[i]
            else:
                result += left_max - height[i]

        right_max = 0
        for i in xrange(len(height)-1, max_height_index, -1):
            if height[i] > right_max:
                right_max = height[i]
            else:
                result += right_max - height[i]

        return result


assert Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) == 6
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容