LeetCode - Trapping Rain Water

Trapping Rain Water

2014.2.10 03:25

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!

Solution:

  The first time I tried to solve this problem, I thought I was supposed to scan the array from left to right, and calculate the amount of water every "pond" can hold. Finding the boundary of every "pond" proved to be difficult, and the code proved to be wrong...(x_x)

  During the second attempt, I changed my way of thinking. Instead of caring about how much water is held in a pond, it would be easier to calculate how much water is held in one grid, it is min(max_height_left_of_here, max_height_right_of_here) - height_of_here.

  Why? Because it has to be high at both ends and low in the middle to hold any water, isn't it?

  If that calculation above yields a negative result, it's simply 0. Because here it is too high, the water flows down and cannot be held.

  In this manner, we just have to calculate how much water is held at every position h[i] and add them up.

  Two extra arrays are needed to record the maximum heights at both ends.

  Therefore, total time complexity is O(n). Space complexity is O(n) as well.

Accepted code:

 1 // 5CE, 2WA, 1AC, "const int &x", constant value has no reference.
 2 class Solution {
 3 public:
 4     int trap(int A[], int n) {
 5         if (A == nullptr || n < 3) {
 6             return 0;
 7         }
 8         
 9         vector<int> vl(n), vr(n);
10         int i;
11         int max_val;
12         int result;
13         
14         max_val = 0;
15         for (i = 0; i <= n - 1; ++i) {
16             vl[i] = max_val;
17             max_val = mymax(max_val, A[i]);
18         }
19         max_val = 0;
20         for (i = n - 1; i >= 0; --i) {
21             vr[i] = max_val;
22             max_val = mymax(max_val, A[i]);
23         }
24         
25         result = 0;
26         for (i = 0; i < n; ++i) {
27             max_val = mymax(0, mymin(vl[i], vr[i]) - A[i]);
28             result += max_val;
29         }
30         vl.clear();
31         vr.clear();
32         
33         return result;
34     }
35 private:
36     int mymin(const int x, const int y) {
37         return (x < y ? x : y);
38     }
39     
40     int mymax(const int x, const int y) {
41         return (x > y ? x : y);
42     }
43 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3542216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值