2023年7月23日每日一题--42. 接雨水

leetcode链接:
https://leetcode.cn/problems/trapping-rain-water/

题目分析

方法一:感觉其和 739 题每日温度的基础上进行延伸,似乎是一条解决问题之道

739.每日温度
https://www.yuanql.top/2023/07/23/02_leetcode/739.%20%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6/

方案一

思考不全面,导致无法通过测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int trap(int[] height) {
int result = 0;

int[] danDiaoDeque = new int[height.length];
Deque<Integer> deque = new ArrayDeque<>();

for (int i = 0; i < height.length; i++) { // 求取高过或等于当前高度所在高度的下一个位置
while (!deque.isEmpty() && height[deque.peek()] <= height[i]) {
Integer pop = deque.pop();
danDiaoDeque[pop] = i - pop;
}
deque.push(i);
}

for (int i = 0; i < height.length;) {
if (danDiaoDeque[i] == 0 || danDiaoDeque[i] == 1) { // 代表此节点和下一个节点之间不可能有沟壑来装雨
i++;
continue;
}
for (int j = i + 1; j < i + danDiaoDeque[i]; j++) {
result += height[i] - height[j];
}
i = i + danDiaoDeque[i];
}

return result;
}
}

结果

解答错误
145 / 322 个通过测试用例

代码随想录

https://programmercarl.com/0042.%E6%8E%A5%E9%9B%A8%E6%B0%B4.html

以下内容均搬上方链接的相关内容,具体请查看原网站: https://programmercarl.com/0042.%E6%8E%A5%E9%9B%A8%E6%B0%B4.html

暴力解法

https://programmercarl.com/0042.%E6%8E%A5%E9%9B%A8%E6%B0%B4.html#%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95

首先要明确,要按照行来计算,还是按照列来计算。

按照行来计算如图:

按照列来计算如图:

双指针优化

https://programmercarl.com/0042.%E6%8E%A5%E9%9B%A8%E6%B0%B4.html#%E5%8F%8C%E6%8C%87%E9%92%88%E4%BC%98%E5%8C%96

单调栈解法

https://programmercarl.com/0042.%E6%8E%A5%E9%9B%A8%E6%B0%B4.html#%E5%8D%95%E8%B0%83%E6%A0%88%E8%A7%A3%E6%B3%95

单调栈就是保持栈内元素有序。和栈与队列:单调队列 (opens new window)一样,需要我们自己维持顺序,没有现成的容器可以用。

通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时我们就要想到可以用单调栈了。

而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。

准备工作

那么本题使用单调栈有如下几个问题:

1. 首先单调栈是按照行方向来计算雨水

按行进行计算的,这点很重要

如图:

知道这一点,后面的就可以理解了。

2. 使用单调栈内元素的顺序

从大到小还是从小到大呢?
从栈头(元素从栈头弹出)到栈底的顺序应该是从小到大的顺序。
因为一旦发现添加的柱子高度大于栈头元素了,此时就出现凹槽了,栈头元素就是凹槽底部的柱子,栈头第二个元素就是凹槽左边的柱子,而添加的元素就是凹槽右边的柱子。
如图:

关于单调栈的顺序给大家一个总结: 739. 每日温度 (opens new window)中求一个元素右边第一个更大元素,单调栈就是递增的,84.柱状图中最大的矩形 (opens new window)求一个元素右边第一个更小元素,单调栈就是递减的。

3. 遇到相同高度的柱子怎么办。

遇到相同的元素,更新栈内下标,就是将栈里元素(旧下标)弹出,将新元素(新下标)加入栈中。
例如 5 5 1 3 这种情况。如果添加第二个5的时候就应该将第一个5的下标弹出,把第二个5添加到栈中。
因为我们要求宽度的时候 如果遇到相同高度的柱子,需要使用最右边的柱子来计算宽度
如图所示:

4. 栈里要保存什么数值

使用单调栈,也是通过 长 * 宽 来计算雨水面积的。

长就是通过柱子的高度来计算,宽是通过柱子之间的下标来计算,

那么栈里有没有必要存一个pair<int, int>类型的元素,保存柱子的高度和下标呢。

其实不用,栈里就存放下标就行,想要知道对应的高度,通过height[stack.top()] 就知道弹出的下标对应的高度了。

所以栈的定义如下:

1
stack<int> st; // 存着下标,计算的时候用下标对应的柱子高度

明确了如上几点,我们再来看处理逻辑。

单调栈处理逻辑

以下逻辑主要就是三种情况

  • 情况一:当前遍历的元素(柱子)高度小于栈顶元素的高度 height[i] < height[st.top()]
  • 情况二:当前遍历的元素(柱子)高度等于栈顶元素的高度 height[i] == height[st.top()]
  • 情况三:当前遍历的元素(柱子)高度大于栈顶元素的高度 height[i] > height[st.top()]

先将下标0的柱子加入到栈中,st.push(0);。 栈中存放我们遍历过的元素,所以先将下标0加进来。

然后开始从下标1开始遍历所有的柱子,for (int i = 1; i < height.size(); i++)

如果当前遍历的元素(柱子)高度小于栈顶元素的高度,就把这个元素加入栈中,因为栈里本来就要保持从小到大的顺序(从栈头到栈底)。

代码如下:

1
if (height[i] < height[st.top()])  st.push(i);

如果当前遍历的元素(柱子)高度等于栈顶元素的高度,要跟更新栈顶元素,因为遇到相相同高度的柱子,需要使用最右边的柱子来计算宽度。

代码如下:

1
2
3
4
if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况
st.pop();
st.push(i);
}

如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示:

取栈顶元素,将栈顶元素弹出,这个就是凹槽的底部,也就是中间位置,下标记为mid,对应的高度为height[mid](就是图中的高度1)。

此时的栈顶元素st.top(),就是凹槽的左边位置,下标为st.top(),对应的高度为height[st.top()](就是图中的高度2)。

当前遍历的元素i,就是凹槽右边的位置,下标为i,对应的高度为height[i](就是图中的高度3)。

此时大家应该可以发现其实就是栈顶和栈顶的下一个元素以及要入栈的元素,三个元素来接水!

那么雨水高度是 min(凹槽左边高度, 凹槽右边高度) - 凹槽底部高度,代码为:int h = min(height[st.top()], height[i]) - height[mid];

雨水的宽度是 凹槽右边的下标 - 凹槽左边的下标 - 1(因为只求中间宽度),代码为:int w = i - st.top() - 1 ;

当前凹槽雨水的体积就是:h * w

求当前凹槽雨水的体积代码如下:

1
2
3
4
5
6
7
8
9
while (!st.empty() && height[i] > height[st.top()]) { // 注意这里是while,持续跟新栈顶元素
int mid = st.top();
st.pop();
if (!st.empty()) {
int h = min(height[st.top()], height[i]) - height[mid];
int w = i - st.top() - 1; // 注意减一,只求中间宽度
sum += h * w;
}
}

关键部分讲完了,整体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
int trap(vector<int>& height) {
if (height.size() <= 2) return 0; // 可以不加
stack<int> st; // 存着下标,计算的时候用下标对应的柱子高度
st.push(0);
int sum = 0;
for (int i = 1; i < height.size(); i++) {
if (height[i] < height[st.top()]) { // 情况一
st.push(i);
} if (height[i] == height[st.top()]) { // 情况二
st.pop(); // 其实这一句可以不加,效果是一样的,但处理相同的情况的思路却变了。
st.push(i);
} else { // 情况三
while (!st.empty() && height[i] > height[st.top()]) { // 注意这里是while
int mid = st.top();
st.pop();
if (!st.empty()) {
int h = min(height[st.top()], height[i]) - height[mid];
int w = i - st.top() - 1; // 注意减一,只求中间宽度
sum += h * w;
}
}
st.push(i);
}
}
return sum;
}
};

以上代码冗余了一些,但是思路是清晰的,下面我将代码精简一下,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int trap(vector<int>& height) {
stack<int> st;
st.push(0);
int sum = 0;
for (int i = 1; i < height.size(); i++) {
while (!st.empty() && height[i] > height[st.top()]) {
int mid = st.top();
st.pop();
if (!st.empty()) {
int h = min(height[st.top()], height[i]) - height[mid];
int w = i - st.top() - 1;
sum += h * w;
}
}
st.push(i);
}
return sum;
}
};

精简之后的代码,大家就看不出去三种情况的处理了,貌似好像只处理的情况三,其实是把情况一和情况二融合了。 这样的代码不太利于理解.

个人编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int trap(int[] height) {
int result = 0;

Deque<Integer> deque = new ArrayDeque<>();

for (int i = 0; i < height.length; i++) {
while (!deque.isEmpty() && height[deque.peek()] <= height[i]) {
Integer pop = deque.pop();
if (!deque.isEmpty()) {
int hg = Math.min(height[deque.peek()], height[i]) - height[pop];
int weight = i - deque.peek() - 1;
result += hg * weight;
}
}
deque.push(i);
}

return result;
}
}

结果

解答成功:
执行耗时:2 ms,击败了41.17% 的Java用户
内存消耗:42.8 MB,击败了63.52% 的Java用户

分析

时间复杂度:
O( n )

空间复杂度:
O( n )

官方题解

https://leetcode.cn/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode-solution-tuvc/

目前点晕,具体情况请看上方链接

方法一:动态规划

方法二:单调栈

方法三:双指针


2023年7月23日每日一题--42. 接雨水
http://yuanql.top/2023/07/23/02_02_leetcode_每日一题/2023年7月23日每日一题--42. 接雨水/
作者
Qingli Yuan
发布于
2023年7月23日
许可协议