LeetCode42 Trapping Rain Water
题目:
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的更多相关文章
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
随机推荐
- mysql如果搜索长度过宽 导致显示不全的情况解决
今天我在搜索数据库里面优惠码字段 直接使用 select * from table 的命令的时候 由于 第一个 字段过长导致后面的都无法显示全..我还是宽屏! 所以 搜索了一下 可以让 它单行显示 使 ...
- JavaScript 链式结构序列化详解
一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } swi ...
- 第三百五十五天 how can I 坚持
快一年了,三百五十五天了,等写个程序算算时间,看看日期和天数能不能对的上,哈哈. 计划还是未制定,天气预报还是没有写完,立马行动,发完这个博客,立马行动. 计划:设计模式1个月,三大框架3个月,计算机 ...
- c++builder XE8 线程 Thread
thread Thread c++builder XE8 / RAD 10 Settle delphi TThread.CreateAnonymousThread(MyMethod).Start; ...
- C++的双冒号(域解析符)
在C++中,“::”表示“作用域标识符”或者叫“作用域分解运算符”,比如:“类名::函数名”,这样是表示该函数是该类的成员函数, 但是象下面这种写法:“::函数名”,作用域标识符前面没有任何对象,代表 ...
- Spring4.0+Hibernate4.0+Struts2.3整合包括增删改查案例,解决整合中出现的异常
源码下载:http://download.csdn.net/detail/cmcc_1234/7034775 ======================Application.xml======== ...
- jquery 应用小结
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- centos下 Apache、php、mysql默认安装路径
centos下 Apache.php.mysql默认安装路径 http://blog.sina.com.cn/s/blog_4b8481f70100ujtp.html apache: 如果采用RPM包 ...
- HDU 2066 一个人的旅行 - from lanshui_Yang
Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...
- sc7731 Android 5.1 LCD驱动简明笔记之三
此篇笔记基于sc7731 - android 5.1,对lcd的gralloc库做一个简明笔记. 第一部分 调用gralloc.sc8830.so所谓的Gralloc模块,它就是一个模块,一个操作ke ...