Skip to content

Commit 95def6f

Browse files
committed
四刷42
1 parent 3e3be6c commit 95def6f

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

docs/0042-trapping-rain-water.adoc

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
[#0042-trapping-rain-water]
2-
= 42. Trapping Rain Water
2+
= 42. 接雨水
33

4-
{leetcode}/problems/trapping-rain-water/[LeetCode - Trapping Rain Water^]
4+
https://leetcode.cn/problems/trapping-rain-water/[LeetCode - 42. 接雨水 ^]
55

6-
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.
6+
给定 `n` 个非负整数表示每个宽度为 `1` 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
77

8-
*Example:*
8+
*示例 1:*
99

1010
image::images/0042-00.png[{image_attr}]
1111

12-
[.small]#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!#
12+
....
13+
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
14+
输出:6
15+
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
16+
....
1317

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [0,1,0,2,1,0,1,3,2,1,2,1]
17-
*Output:* 6
18-
----
18+
*示例 2:*
19+
20+
....
21+
输入:height = [4,2,0,3,2,5]
22+
输出:9
23+
....
24+
25+
*提示:*
1926

27+
* `n == height.length`
28+
* `1 \<= n \<= 2 * 10^4^`
29+
* `0 \<= height[i] \<= 10^5^`
30+
31+
32+
[#解题分析]
2033
== 解题分析
2134

35+
单调栈解法图示:
36+
2237
image::images/0042-01.png[{image_attr}]
2338

2439
image::images/0042-02.png[{image_attr}]
@@ -61,6 +76,15 @@ include::{sourcedir}/_0042_TrappingRainWater_2.java[tag=answer]
6176
include::{sourcedir}/_0042_TrappingRainWater_3.java[tag=answer]
6277
----
6378
--
79+
80+
四刷::
81+
+
82+
--
83+
[{java_src_attr}]
84+
----
85+
include::{sourcedir}/_0042_TrappingRainWater_4.java[tag=answer]
86+
----
87+
--
6488
====
6589

6690

@@ -70,6 +94,7 @@ include::{sourcedir}/_0042_TrappingRainWater_3.java[tag=answer]
7094

7195
== 参考资料
7296

97+
. https://leetcode.cn/problems/trapping-rain-water/solutions/692342/jie-yu-shui-by-leetcode-solution-tuvc/[42. 接雨水 - 官方题解^] -- 左右双指针的解法,代码最简单。
7398
. https://leetcode.cn/problems/trapping-rain-water/solutions/9112/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-w-8/[42. 接雨水 - 详细通俗的思路分析,多解法^]
7499
. https://blog.csdn.net/weixin_50348837/article/details/136304458[深入理解单调栈算法,这一篇就够了^]
75100
. https://leetcode.cn/problems/trapping-rain-water/solutions/185678/trapping-rain-water-by-ikaruga/[42. 接雨水 - 单调递减栈,简洁代码,动图模拟^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ endif::[]
368368
|{doc_base_url}/0027-remove-element.adoc[题解]
369369
|✅ 快慢指针。慢指针记录满足条件的长度,快指针指向需要处理的元素。
370370

371+
|{counter:codes}
372+
|{leetcode_base_url}/trapping-rain-water/[42. 接雨水^]
373+
|{doc_base_url}/0042-trapping-rain-water.adoc[题解]
374+
|❌ 使用单调栈没写出来。使用单调栈,需 `h × w`,不能只看头顶的容量。使用左右双指针夹逼代码更简单,只需要计算头顶的容量即可。
375+
371376
|===
372377

373378
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0042_TrappingRainWater_4 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-04-20 18:38:35
12+
*/
13+
public int trap(int[] height) {
14+
Deque<Integer> stack = new ArrayDeque<>(height.length);
15+
int result = 0;
16+
for (int right = 0; right < height.length; right++) {
17+
while (!stack.isEmpty() && height[stack.peek()] < height[right]) {
18+
int mid = stack.poll();
19+
if (stack.isEmpty()) {
20+
break;
21+
}
22+
Integer left = stack.peek();
23+
int h = Math.min(height[left], height[right]) - height[mid];
24+
int w = right - left - 1;
25+
result += h * w;
26+
}
27+
stack.push(right);
28+
}
29+
return result;
30+
}
31+
32+
// end::answer[]
33+
public static void main(String[] args) {
34+
new _0042_TrappingRainWater_4().trap(new int[]{4, 2, 0, 3, 2, 5});
35+
}
36+
}

0 commit comments

Comments
 (0)