leetcode Trapping Rain Water

原创 2013年12月02日 22:19:12

Trapping Rain Water

 Total Accepted: 2335 Total Submissions: 8464My Submissions

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!

Discuss



Find the highest, then from the start to the highest, then the last to the highest..

class Solution {
 public:
  int trap(int A[], int n) {
    if (n <= 2)
      return 0;
    int i, maxElevation = A[0], maxIndex = 0, h = 0, res = 0;
    for (i = 1; i < n; ++i)
      if (A[i] > maxElevation) {
        maxElevation = A[i];
        maxIndex = i;
      }
    for (i = 0; i <= maxIndex - 1; ++i) 
      if (A[i] >= h)
        h = A[i];
      else
        res += (h - A[i]);
    
    h = 0;
    for (i = n - 1; i >= maxIndex + 1; --i) 
      if (A[i] >= h)
        h = A[i];
      else
        res += (h - A[i]);
    return res;
  }
};


LeetCode------42. Trapping Rain Water(捕获雨水量)

Given n non-negative integers representing an elevation map where the width of each bar is 1, comput...
  • nameix
  • nameix
  • 2016年10月13日 13:25
  • 473

[C++]LeetCode: 131 Trapping Rain Water (双边扫描)

题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, c...
  • cinderella_niu
  • cinderella_niu
  • 2015年02月04日 10:30
  • 771

Trapping Rain Water -- LeetCode

原题链接: http://oj.leetcode.com/problems/trapping-rain-water/  这道题比较直接的做法类似Longest Palindromic Substri...
  • linhuanmars
  • linhuanmars
  • 2014年03月10日 03:50
  • 16841

[LeetCode]42.Trapping Rain Water

【题目】 Given n non-negative integers representing an elevation map where the width of each bar is 1,...
  • SunnyYoona
  • SunnyYoona
  • 2014年01月20日 16:45
  • 2400

【LeetCode】Trapping Rain Water解题报告

【题目】 Given n non-negative integers representing an elevation map where the width of each bar is...
  • ljiabin
  • ljiabin
  • 2014年12月08日 15:19
  • 4239

Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告

留坑还木有来得及做,还要赶明天的东西,这个Hard应该很费时间,所以保持队形。。这个问题其实之前也有过: Leetcode #42. Trapping Rain Water 雨水收集 解题报告 只不...
  • MebiuW
  • MebiuW
  • 2016年09月25日 21:54
  • 3374

[leetcode] 407. Trapping Rain Water II 解题报告

题目链接:https://leetcode.com/problems/trapping-rain-water-ii/ Given an m x n matrix of positive integ...
  • qq508618087
  • qq508618087
  • 2016年09月29日 06:25
  • 2381

【LeetCode with Python】 Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, comput...
  • nerv3x3
  • nerv3x3
  • 2014年07月06日 15:38
  • 5690

LeetCode --- 42. Trapping Rain Water

题目链接:Trapping Rain Water Given n non-negative integers representing an elevation map where the widt...
  • makuiyu
  • makuiyu
  • 2015年02月08日 23:49
  • 2463

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

题目链接: https://leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representin...
  • qq508618087
  • qq508618087
  • 2016年05月05日 12:55
  • 441
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Trapping Rain Water
举报原因:
原因补充:

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