随笔- 509  文章- 0  评论- 151  阅读- 22万 

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 };
复制代码

 

 posted on   zhuli19901106  阅读(271)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示