题目:

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(07)转化流

    查看API得知: public class OutputStreamWriter extends Writer public class InputStreamReader extends Reade ...

  2. cisco tftp 备份/恢复

    使用tftp服务器对cisco 3560 配置备份及恢复 Switch#copy running-config tftp:Address or name of remote host []? 192. ...

  3. Maven Archetype Plugin

    使用Archetype的一般步骤 命令——mvn archetype:generate 输入命令后,Archetype插件会输出一个Archetype列表供用户选择:选择自己想要使用的Archetyp ...

  4. VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】

    Visual Studio 2008 .VS2008.VC2008工程源文件配置.编译配置   有时编写代码时,往往存在这样的需求(或是希望有这样的功能):一个工程经过不共同的配置实现不同的版本或是功 ...

  5. FZU 8月有奖月赛A Daxia & Wzc's problem (Lucas)

    Problem A Daxia & Wzc's problem Accept: 42    Submit: 228Time Limit: 1000 mSec    Memory Limit : ...

  6. JAX-RS入门 一 :基础

    简介 JAX-RS是一套用java实现REST服务的规范,提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源.标注包括: @Path,标注资源类或方法的相对路径 @GET,@PUT, ...

  7. DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化

    http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...

  8. 一位Erlang程序猿的自白

    12.00 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style De ...

  9. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  10. 在编辑模式中一个ASP.NET应用详细视图显示集合属性 编辑模式和只读模式

    https://documentation.devexpress.com/#Xaf/CustomDocument3230