题目:

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.

题解:

  遍历数组,找到局部最小值,方法是如果当前值大于或等于前一个值,或者当前值大于后一个值则跳过。找到了局部最小值后,然后向左找左边的最大值,再向右找右边的最大值,找右边最大值时要注意当其大于左边最大时就停止寻找。然后算出从左边最大值到右边最大值之间能装的水量,之后从右边最大值的位置开始继续找局部最小值,以此类推直到遍历完整个数组。(from here)Solution 1 中height[i] > height[i + 1]是为了避免若为>=,则数组为[5,5,1,7, 1, 1, 5,2,7,6]情况,没有=为23,有=为7;

Solution 1()

class Solution {
public:
int trap(vector<int>& height) {
int result = , n = height.size(), left = , right = ;
for(int i=; i<n-; i++) {
if(height[i] >= height[i-] || height[i] > height[i+]) continue;
for(left = i-; left>; --left) {
if(height[left]>=height[left-]) break;
}
right = i + ;
for(int j=i+; j<n; ++j) {
if(height[j]>=height[right]) {
right = j;
if(height[right]>=height[left]) break;
}
}
int h = min(height[left],height[right]);
for(int k=left+; k<right; ++k) {
if(h>height[k]) result += h - height[k];
}
i = right;
}
return result;
}
};

  对于每一个值,其与另外两个值组成的容器收集的雨水最大量肯定是在其左右两边的最大值作为容器的两个壁的情况下获得的,具体在题目里就是这两个最大值的较小值与当前值的差(当这个最小值大于当前值时,否则收集不到雨水)。使用动态规划,初始化一维数组vector<int> dp(n,0)。然后遍历两遍数组,第一次遍历从左边扫描找出当前位置左边的最大值,并存放到dp中;第二次遍历从右边扫描找出当前位置右边的最大值,然后与dp中保存的左边最大值比较,存下二者之中的较小值,并且将这个值与当前值比较,如果大于当前值,则收集的雨水总量应该加上这个较小值与当前值的差值。用一个数组 left[i] 表示第 i 根柱子左边最高的柱子的高度(from here
  用一个数组 right[i] 表示第 i 根柱子右边最高的柱子的高度
  1.从左到右扫描,left[i] = max(left[i - 1], A[i - 1])
  2.从右到左扫描,right[i] = max(right[i + 1], A[i + 1])
  3.第 i 根柱子上能储蓄的水为 min(left[i], right[i]) - A[i],因为这时 left[i] 已经没用了,可以用它来存放这个值。(from here
复杂度:时间O(n), 空间O(n)

Solution 2 ()  

class Solution {
public:
int trap(vector<int>& height) {
int result = , lmax = , rmax = , n = height.size();
vector<int> dp(n,);
for(int i=; i<n; i++) {
dp[i] = lmax;
lmax = max(lmax, height[i]);
}
for(int i=n-; i>=; i--) {
dp[i] = min(dp[i], rmax);
rmax = max(rmax, height[i]);
if(dp[i] > height[i]) {
result += dp[i] - height[i];
}
}
return result;
}
};

  根据上面的雨水收集的分析,任意3个值组成的雨水收集量即是两边的较小值与中间值的差值(如果前者大于后者)。因此可以设置两个指针从两头往中间遍历,这两个指针所指的值即为容器的两个壁。低的壁(较小值)决定了收集雨水的量,因此中间值设为从较小值的后续值,并且算出收集的雨水,直到遇到一个值大于这个较小值,并且更新较小值为当前值。这样只需一趟扫描就可以解决问题。(from here

Solution 3 ()

class Solution {
public:
int trap(vector<int>& height) {
int result = , left = , right = height.size() - ;
while (left < right) {
int h = min(height[left], height[right]);
if (h == height[left]) {
++left;
while (left < right && height[left] < h) {
result += h - height[left++];
}
} else {
--right;
while (left < right && height[right] < h) {
result += h - height[right--];
}
}
}
return result;
}
};

  The idea is:

  1) find the highest bar.

  2) traverse the bar from left the highest bar.becasue we have the highest bar in right, so, any bar higher than its right bar(s) can contain the water.

  3) traverse the bar from right the highest bar.becasue we have the highest bar in left, so, any bar higher than its left bar(s) can contain the water. (from here ChenHao)

Solution 4 ()

class Solution {
public:
int trap(vector<int>& height) {
int result = , maxhigh = , maxidx = , n = height.size();
for(int i=; i<n; i++) {
if(height[i] > maxhigh) {
maxhigh = height[i];
maxidx = i;
}
}
int prehigh = ;
for(int i=; i<maxidx; ++i) {
if(height[i] > prehigh) {
prehigh = height[i];
}
result += (prehigh - height[i]);
}
prehigh = ;
for(int i=n-; i>maxidx; --i) {
if(height[i] > prehigh) {
prehigh = height[i];
}
result += (prehigh - height[i]);
} return result;
}
};

Solution 5 ()

class Solution {
int trap(vector<int>& height) {
  int l = , r = height.size()-, level = , water = ;
  while (l < r) {
  int lower = height[height[l] < height[r] ? l++ : r--];
  level = max(level, lower);
  water += level - lower;
  }
  return water;
  }
}

【LeetCode】042 Trapping Rain Water的更多相关文章

  1. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  2. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  3. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  4. 【Lintcode】364.Trapping Rain Water II

    题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...

  5. 【Lintcode】363.Trapping Rain Water

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

  6. LeetCode 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

  7. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  8. 【LeetCode题意分析&解答】42. Trapping Rain Water

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

  9. LeetCode OJ 42. Trapping Rain Water

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

随机推荐

  1. CentOS开启FTP及配置用户

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  2. Spring Security实现短信验证码登录

    Spring Security默认的一个实现是使用用户名密码登录,当初我们在开始做项目时,也是先使用这种登录方式,并没有多考虑其他的登录方式.而后面需求越来越多,我们需要支持短信验证码登录了,这时候再 ...

  3. unbuntu16.04上python开发环境搭建建议

    unbuntu16.04上python开发环境搭建建议  2017-12-20  10:39:27 推荐列表: pycharm: 可以自行破解,但是不推荐,另外也不稳定 pydev+eclipse: ...

  4. Urho3D 在Win10下编辑器崩溃的解决方案

    本解决方案来自于 https://github.com/urho3d/Urho3D/issues/2417 描述 在Win10中通过CMake启用URHO_ANGELSCRIPT选项的前提下生成Urh ...

  5. 可执行Jar包调用动态链接库(DLL/SO)

    踩过了很多的坑,查了很多资料,在此记录一下,以SpringBoot项目为基础. Maven加入JNA依赖 <!-- JNA start --> <dependency> < ...

  6. linux查看磁盘挂载的三种方法

    第一种方法:使用df命令,例如: orientalson:/home # df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda ...

  7. 数据结构:最小生成树--Kruskal算法

    Kruskal算法 Kruskal算法 求解最小生成树的还有一种常见算法是Kruskal算法.它比Prim算法更直观.从直观上看,Kruskal算法的做法是:每次都从剩余边中选取权值最小的,当然,这条 ...

  8. Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现

    看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...

  9. wepy项目中使用async await

    https://github.com/Tencent/wepy/wiki/wepy项目中使用async-await

  10. BZOJ4920: [Lydsy1706月赛]薄饼切割

    BZOJ4920: [Lydsy1706月赛]薄饼切割 Description 有一天,tangjz送给了quailty一张薄饼,tangjz将它放在了水平桌面上,从上面看下去,薄饼形成了一个H*W的 ...