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 ...
随机推荐
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- LINQ标准查询操作符(四) —AsEnumerable,Cast,OfType,ToArray,ToDictionary,ToList,ToLookup,First,Last,ElementAt
十.转换操作符 转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以“As”开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以“To”开头的方法可枚举(即时加载)源集合并 ...
- MS 数据库存储过程加密解密
存储过程加密解密在网上有很多,刚刚好最近需要用到,所以就查询了一下资料.记录一下 加密方法:执行如下存储过程 DECLARE @sp_name nvarchar(400) DECLARE @sp_co ...
- RingBuffer源代码分析
看到一篇写的非常详细的帖子,为防止楼主删帖后找不到,果断转载过来 RingBuffer源代码分析 出处:http://bbs.ickey.cn/community/forum.php?mod=view ...
- Flipping Parentheses(CSU1542 线段树)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1542 赛后发现这套题是2014东京区域赛的题目,看了排名才发现自己有多low = =! 题目大意 ...
- LRESULT与wParam和lParam的问题
在微软vc提供的头文件中有定义在winnt.h中typedef long LONG;在windef.h中typedef LONG LRESULT; 所以LRESULT就是long,也就是长整形之所以取 ...
- 关于IE开发人员工具(F12)找不到的问题
关于IE开发人员工具(F12)找不到的问题 解决方案:第一步,像往常一样F12或者,工具->开发人员工具,点击后,这个时候你是看不到工具界面(当然,如果你正好遇到了找不到这个问题);第二步,这个 ...
- Java 线程池学习
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...
- Unable to generate a temporary class (result=1)解决方法
Unable to generate a temporary class (result=1).error CS2001: Source file 'C:\WINDOWS\TEMP\ug5v9uxt. ...
- VS2010 远程调试
1.在客户端电脑建一个账户,账户名和密码和调试端的账户密码一样 2.在客户端电脑进入 管理工具-本地安全策略-本地策略-安全选项 网络访问:本地账户的共享和安全模式”,改为“经典-本地用户以自己的身份 ...