LeetCode 42 Trapping Rain Water

原创 2016年08月30日 09:42:23

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,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!


题目链接:https://leetcode.com/problems/trapping-rain-water/

题目分析:考虑到存在高度差才会积水,所以先找最高的那个,然后分别从两边往最高的前进,并记录当前状态下的最高值,只要当前高度小于当前最高值,必然会积水,积水的面积就是高度差

public class Solution {
    public int trap(int[] height) {
        int maxHeight = 0, len = height.length, pos = 0;
        for(int i = 0; i < len; i ++) {
            if(maxHeight < height[i]) {
                pos = i;
                maxHeight = height[i];
            }
        }
        int ans = 0;
        int curMax = 0, curArea = 0;
        for(int i = 0; i < pos; i ++) {
            if(height[i] > curMax) {
                curMax = height[i];
            }
            else {
                curArea += curMax - height[i];
            }
        }
        ans += curArea;
        curMax = 0;
        curArea = 0;
        for(int i = len - 1; i > pos; i --) {
            if(height[i] > curMax) {
                curMax = height[i];
            }
            else {
                curArea += curMax - height[i];
            }
        }
        ans += curArea;
        return ans;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode || 42、 Trapping Rain Water

problem: Given n non-negative integers representing an elevation map where the width of each...

[leetcode]42. Trapping Rain Water(Java)

https://leetcode.com/problems/trapping-rain-water/#/description Given n non-negative inte...

Leetcode-42 Trapping in Rain Water

题目:             示例输入:                                                                              ...
  • qzq2514
  • qzq2514
  • 2017年03月18日 11:18
  • 248

[Leetcode] 42. Trapping Rain Water 解题报告

题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

leetcode 42. Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, comput...

[Leetcode 42, Hard] Trapping Rain Water

[Leetcode 42, Hard] Trapping Rain Water

Leetcode #42. Trapping Rain Water 雨水收集 解题报告

1 解题思想原题给定一个X轴上的取值,可以视为一个凸起的墙,高度就是对应的Y值,现在问如果下了一场雨,那么这些凸起的地方一共可以收集到多少的雨水?那么如何才能收集呢?从图片上可以推出的就是,任意两个凸...
  • MebiuW
  • MebiuW
  • 2016年04月13日 23:24
  • 846

Leetcode42. Trapping Rain Water

Leetcode42 trapping rain water
  • Earl211
  • Earl211
  • 2017年06月29日 09:56
  • 126

leetcode 42 Trapping Rain Water

Trapping Rain Water Total Accepted: 47928 Total Submissions: 158168 Difficulty: Hard Given n...
  • javays1
  • javays1
  • 2015年09月22日 10:59
  • 288

[LeetCode]42.Trapping Rain Water

【题目】 Given n non-negative integers representing an elevation map where the width of each bar is 1,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 42 Trapping Rain Water
举报原因:
原因补充:

(最多只允许输入30个字)