algorithm-base/animation-simulation/单调队列单调栈/接雨水.md

152 lines
8.5 KiB
Java
Raw Normal View History

2021-07-23 15:44:19 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-03-20 11:38:55 +00:00
>
>
>
2021-07-23 15:44:19 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 05:38:47 +00:00
2021-03-20 11:38:55 +00:00
#### [42. ](https://leetcode-cn.com/problems/trapping-rain-water/)
2021-03-19 08:36:59 +00:00
n 1
1
```
height = [0,1,0,2,1,0,1,3,2,1,2,1]
6
```
2
```
height = [4,2,0,3,2,5]
9
```
2021-07-23 15:44:19 +00:00
3
2021-03-19 08:36:59 +00:00
```
[4,3,2,0,1,1,5]
13
```
2021-07-23 15:44:19 +00:00
> [4,3,2,0,1,1,5] 13
2021-03-19 08:36:59 +00:00
###
2021-07-23 15:44:19 +00:00
3
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/2021032013412768.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
4 23 4
2021-03-19 08:36:59 +00:00
2021-03-20 05:45:03 +00:00
<img src="https://img-blog.csdnimg.cn/20210320134154434.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述" style="zoom:80%;" />
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
3
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
4320 1 0 0 1
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134213324.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
4320 1 0 2201 ()
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134228696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
0 1 4321 1 43211 5 1 1 2
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134249605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
2115 1\*3=3;1 min(2-1,5-1)=min(1,4) 13 5 62 2345 3 6-2-1=3
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
1 2 3 325
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134307389.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
4 2 435
2021-03-19 08:36:59 +00:00
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134319646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
2021-07-23 15:44:19 +00:00
5
2021-03-19 08:36:59 +00:00
1+3+4+5=13
![](https://img-blog.csdnimg.cn/20210319163622150.gif)
###
```java
class Solution {
public int trap(int[] height) {
Stack<Integer> stack = new Stack<Integer>();
int water = 0;
//特殊情况
if(height.length <3){
return 0;
2021-07-23 15:44:19 +00:00
}
2021-03-19 08:36:59 +00:00
for(int i = 0; i < height.length; i++){
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
//栈顶元素
int popnum = stack.pop();
//相同元素的情况例11
while(!stack.isEmpty()&&height[popnum] == height[stack.peek()]){
stack.pop();
}
//计算该层的水的单位
if(!stack.isEmpty()){
int temp = height[stack.peek()];//栈顶元素值
//高
int hig = Math.min(temp-height[popnum],height[i]-height[popnum]);
//宽
int wid = i-stack.peek()-1;
water +=hig * wid;
}
}
//这里入栈的是索引
stack.push(i);
}
return water;
}
}
```
2021-07-27 18:26:32 +00:00
GO Code:
```go
func trap(height []int) int {
stack := []int{}
water := 0
// 最左边部分不会接雨水左边持续升高时stack都会弹出所有元素。
for i := 0; i< len(height); i++ {
for len(stack) != 0 && height[i] > height[stack[len(stack) - 1]] {
popnum := stack[len(stack) - 1]
// 出现相同高度的情况其实也可以不用处理如果不处理相同高度时后面的hig为0会产生很多无效的计算
for len(stack) != 0 && height[popnum] == height[stack[len(stack) - 1]] {
stack = stack[:len(stack) - 1]
}
if len(stack) == 0 { break }
le, ri := stack[len(stack) - 1], i
hig := min(height[ri], height[le]) - height[popnum]
wid := ri - le - 1
water += wid * hig
}
stack = append(stack, i)
}
return water
}
func min(a, b int) int {
if a < b { return a }
return b
}
```
2021-07-23 15:44:19 +00:00
###