题目:

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,只不过这次可以直接从数组里读左右最小值结果。

代码:

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

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

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

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

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

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

代码:

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

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. 《Java数据结构与算法》笔记-CH5-链表-2单链表,增加根据关键字查找和删除

    /** * Link节点 有数据项和next指向下一个Link引用 */ class Link { private int iData;// 数据 private double dData;// 数据 ...

  2. JVM系列一:JVM内存组成及分配

    java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...

  3. ArcGIS Desktop打开慢的解决办法

    问题:ArcGIS Desktop打开巨慢,不管是ArcMap还是CataLog都是一样的,打开一次有时候需要10多分钟. 解决方法:C:\Users\[用户名]\AppData\Roaming\ES ...

  4. $watch How the $apply Runs a $digest

    作者:junyuecao | 发表于 8-8 13:39 | 最后更新时间:8-9 02:34 原文地址:http://angular-tips.com/blog/2013/08/watch-how- ...

  5. C++11用于元编程的类别属性

    [C++11用于元编程的类别属性] 许多算法能作用在不同的数据类别; C++ 模板支持泛型,这使得代码能更紧凑和有用.然而,算法经常会需要目前作用的数据类别的信息.这种信息可以通过类别属性 (type ...

  6. JAVA应用apache httpclient探测http服务

    代码很简单,apache都已经提供了封装. import org.apache.commons.httpclient.HttpClient; import org.apache.commons.htt ...

  7. String.valueOf(null) 报空指针

    String.valueOf 默认的方法 argument 可以为null 的 boolean b = null; char c = null; char[] data = null; double ...

  8. C# 获取字符串中的数字

    /// <summary> /// 获取字符串中的数字(不包含小数点) /// </summary> /// <param name="str"> ...

  9. sc7731 Android 5.1 LCD驱动简明笔记之一

    基于展讯sc7731 - Android 5.1 代码分析浏览.将屏蔽细节,把握整体,并且不涉及其他设备和LCD的交互. 以下对sc7731 lcd大体流程进行简要说明. 第一,lcd 的两个阶段 1 ...

  10. IE调试方法(一)<转>

    前面两篇关于IE11开发人员工具的文章,我们分别介绍了两个新的功能:UI响应工具和内存分析工具,今天为大家介绍一个老功能:网络工具,虽然是在IE9开始已经加入了这个工具,但是在IE11中还有有很多改进 ...