题目:

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.  (Hard)

分析:

首先考虑对每个位置向左向右找到最小值,然后与本位置的值比较添加存水结果,可以做出来,时间复杂度O(n^2);

考虑利用空间优化时间,开两个数组,分别存当前位置向左的最小值和向右的最小值。

一次从左往右遍历更新leftHeight数组, 一次从右向左遍历更新rightHeight数组,最后一次遍历算利用上述方法算result,只不过这次可以直接从数组里读左右最小值结果。

代码:

  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. int leftMax[height.size()];
  5. leftMax[] = ;
  6. for (int i = ; i < height.size(); ++i) {
  7. leftMax[i] = max(height[i - ], leftMax[i - ]);
  8. }
  9. int rightMax[height.size()];
  10. rightMax[height.size() - ] = ;
  11. for (int i = height.size() - ; i >= ; --i) {
  12. rightMax[i] = max(height[i + ], rightMax[i + ]);
  13. }
  14. int result = ;
  15. for (int i = ; i < height.size(); ++i) {
  16. if (min(leftMax[i], rightMax[i]) - height[i] > ) {
  17. result += (min(leftMax[i], rightMax[i]) - height[i]);
  18. }
  19. }
  20. return result;
  21. }
  22. };

上述方法将时间复杂度优化到了O(n),但利用了额外的空间,空间复杂度也提高到了O(n)。

进一步优化,可以考虑Two pointers的思路。

两根指针分别指向头和尾,并维护两个值,表示从左向右到p1的最大值leftHeight和从右向左到p2的最大值RightHeight。

两个最大值中较小的向中间移动,遇到更大的值更新leftHeight或rightHeight,遇到较小的值更新result。

这样可以做到时间复杂度O(n),空间复杂度O(1)。

代码:

  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. if (height.size() < ) {
  5. return ;
  6. }
  7. int left = , right = height.size() - ;
  8. int leftHeight = height[], rightHeight = height[height.size() - ];
  9. int result = ;
  10. while (left < right) {
  11. if (leftHeight <= rightHeight) {
  12. left++;
  13. if (height[left] < leftHeight) {
  14. result += (leftHeight - height[left]);
  15. }
  16. else {
  17. leftHeight = height[left];
  18. }
  19. }
  20. else {
  21. right--;
  22. if (height[right] < rightHeight) {
  23. result += (rightHeight - height[right]);
  24. }
  25. else {
  26. rightHeight = height[right];
  27. }
  28. }
  29. }
  30. return result;
  31. }
  32. };
 

LeetCode42 Trapping Rain Water的更多相关文章

  1. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  2. [LeetCode] Trapping Rain Water 收集雨水

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

  3. [LintCode] Trapping Rain Water 收集雨水

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

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  6. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

  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的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  9. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

随机推荐

  1. c语言分析函数调用关系图(call graph)的几种方法

    一.基于 Doxygen或 lxr 的API形式的文档系统. 二.基于CodeViz, CodeViz是<Understanding The Linux Virtual Memory Manag ...

  2. 第二百三十五天 how can I 坚持

    其实昨天听遗憾的,尽头看了新闻,有好多人都出去赏雪了,可惜了,最遗憾的是没有叫上你一块去. 晚上喝了点酒,抽了两根烟,以前基本不喝酒,就别提抽烟了,陈小春的<算你狠>,该如何是好. 经常在 ...

  3. CentOS下MySQL 5.7.9编译安装

    MySQL 5.7 GA版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 更好的性能:对于多核CPU.固态硬盘.锁有着更好的优化 ...

  4. CST和GMT时间的区别

    CST和GMT时间的区别 今天遇到一个奇怪的问题,在服务器端通过 c# 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为 GMT时间为:28 Aug 2009 01: ...

  5. HTML5边玩边学(1)画布实现方法

    一.<canvas>标签 Html5 引入了一个新的 <canvas> 标签,这个标签所代表的区域就好象一块画布,你的所有图形绘制最后都要在这块画布上呈现.有了这个标签,浏览器 ...

  6. ArcObjects10.0引用控件报错

    错误如下:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS comp ...

  7. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  8. MFC消息响应机制 q

    MFC消息响应机制分析 1 引言微软公司提供的MFC基本类库(Microsoft Foundation Classes),是进行可视化编程时使用最为流行的一个类 库.MFC封装了大部分Windows ...

  9. 手把手教你玩转SOCKET模型之重叠I/O篇(下)

    四.     实现重叠模型的步骤 作 了这么多的准备工作,费了这么多的笔墨,我们终于可以开始着手编码了.其实慢慢的你就会明白,要想透析重叠结构的内部原理也许是要费点功夫,但是只是学会 如何来使用它,却 ...

  10. 结构类模式(七):代理(Proxy)

    定义 为其他对象提供一种代理以控制对这个对象的访问. 代理模式也叫做委托模式,它是一项基本设计技巧.许多其他的模式,如状态模式.策略模式.访问者模式本质上是在更特殊的场合采用了委托模式,而且在日常的应 ...