描述
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. Thanks Marcos for contributing this image!
思路
- 第一种是找水位,具体做法是对于每个点,往左往右分别找出最高值,然后计算取较小者,这就是水位高度。减去当前点的高度就是水的体积。
class Solution {
public int trap(int[] height){
int ans = 0;
for (int i = 1;
i < height.length;
i++){
int left = 0, right = 0;
for(int j = i;
j >= 0;
j--) {
left = Math.max(left, height[j]);
}
for(int j = i;
j < height.length;
j++){
right = Math.max(right, height[j]);
}
ans += Math.min(left, right) - height[i];
}
return ans;
}
}
- 我们发现上面的算法,有着很多重复计算,只要把结果缓存一下,就可以变成O(N)
public int trap(int[] height){
if (height == null)
return 0;
int ans = 0;
int size = height.length;
int[] left = new int[size];
int[] right = new int[size];
left[0] = height[0];
for (int i = 1;
i < size;
i++){
left[i] = Math.max(height[i], left[i-1]);
}
right[size - 1] = height[size - 1];
for (int i = size - 2;
i >= 0;
i--){
right[i] = Math.max(height[i], right[i+1]);
}
for (int i = 1;
i < size - 1;
i++){
ans += Math.min(left[i], right[i]) - height[i];
}
return ans;
}
- 时间复杂度肯定是最优了,之后只能优化空间复杂度了。
public int trap(int[] height){
int left = 0;
int right = height.length - 1;
int leftMax = 0, rightMax = 0;
int ans = 0;
while (left < right){
if (height[left] < height[right]){
if (height[left] >= leftMax)
leftMax = height[left];
else
ans += leftMax - height[left];
left++;
}else{
if (height[right] >= rightMax)
rightMax = height[right];
else
ans += rightMax - height[right];
right--;
}
}
return ans;
}
【Leetcode 42. Trapping Rain Water】
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/a0a08b603fa94718ba0dc621838888c6.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/cd46e3cbd1d945e6ac12b986184ca7e6.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/c823eb8d3d6d4bb2a776045579c9b5c1.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/c06c0cba0a4646749d09e0a8c451c923.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/10145d892b4d4159983da7cdd12703cd.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/50ff38a869f0453f87888023a126c1fd.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/67f2a61e674240c5a2587209527a56d1.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/5b038634d94048179de7d1a8586ed967.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/503cd4b6cf0b4641aa26c77b45b5b612.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/b0c21789bb4a4fbabd5ce5ea054e9378.jpg)
文章图片
![Leetcode 42. Trapping Rain Water](https://img.it610.com/image/info8/e4ac449b27bd4eb2a1ea8abf7bdaec58.jpg)
文章图片
推荐阅读
- 人工智能|干货!人体姿态估计与运动预测
- 分析COMP122 The Caesar Cipher
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储
- Python机器学习基础与进阶|Python机器学习--集成学习算法--XGBoost算法
- 数据结构与算法|【算法】力扣第 266场周赛
- 数据结构和算法|LeetCode 的正确使用方式
- leetcode|今天开始记录自己的力扣之路
- 人工智能|【机器学习】深度盘点(详细介绍 Python 中的 7 种交叉验证方法!)
- 网络|简单聊聊压缩网络