Skip to content

Commit 1fc4545

Browse files
committed
✨ (solution): Problem 42. Trapping Rain Water
Add Solution of 42. Trapping Rain Water
1 parent f2f64ef commit 1fc4545

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

.github/workflows/code-check.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Code Quality Check
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
python-check:
11+
name: Python Code Check
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.x'
21+
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install ruff
26+
27+
- name: Run Ruff
28+
run: |
29+
# Run ruff on Python files in solutions directory
30+
ruff check ./solutions

pyproject.toml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[tool.ruff]
2+
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
3+
select = ["E", "F"]
4+
ignore = []
5+
6+
# Allow autofix for all enabled rules (when `--fix`) is provided.
7+
fixable = ["A", "B", "C", "D", "E", "F"]
8+
unfixable = []
9+
10+
# Exclude a variety of commonly ignored directories.
11+
exclude = [
12+
".bzr",
13+
".direnv",
14+
".eggs",
15+
".git",
16+
".git-rewrite",
17+
".hg",
18+
".mypy_cache",
19+
".md",
20+
".nox",
21+
".pants.d",
22+
".pytype",
23+
".ruff_cache",
24+
".svn",
25+
".tox",
26+
".venv",
27+
"__pypackages__",
28+
"_build",
29+
"buck-out",
30+
"build",
31+
"dist",
32+
"node_modules",
33+
"venv",
34+
]
35+
36+
# Same as Black.
37+
line-length = 88
38+
39+
# Allow unused variables when underscore-prefixed.
40+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
41+
42+
# Assume Python 3.12
43+
target-version = "py312"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
comments: true
3+
difficulty: easy
4+
# Follow `Topics` tags
5+
tags:
6+
- Array
7+
- Two Pointers
8+
- Dynamic Programming
9+
- Stack
10+
- Monotonic Stack
11+
---
12+
13+
# [42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)
14+
15+
## Description
16+
17+
Given an elevation map with width of 1 which representing by `n` non-negative integers. Calculate amount of water that can be trapped after rainfall.
18+
19+
**Example 1:**
20+
```
21+
Input: height = [0,1,0,0,1,0,1,3,2,0,1]
22+
Output: 4
23+
Explanation: [1,0,0,1] can trapped 2 units, [1,0,1] can trapped 1 units, [2,0,1] can trapped 1 units.
24+
```
25+
26+
**Example 2:**
27+
```
28+
Input: height = [4,1,2,4,5]
29+
Output: 5
30+
Explanation: [4,1,2,4] can trapped 5 units.
31+
```
32+
33+
34+
**Constraints:**
35+
`n == height.length`
36+
`1 <= n <= 2 * 10^4`
37+
`0 <= height[i] <= 10^5`
38+
39+
## Solution
40+
41+
Shrink from the border. If the outside is higher than the inside, rain will accumulate. Therefore, the difference between the higher and lower bars represents the amount of retained rainwater.
42+
43+
44+
```java
45+
class Solution {
46+
public int trap(int[] height) {
47+
int left = 0, right = height.length - 1;
48+
int leftMax = height[left], rightMax = height[right];
49+
int water = 0;
50+
51+
while (left < right) {
52+
if (leftMax < rightMax) {
53+
left++;
54+
leftMax = Math.max(leftMax, height[left]);
55+
water += leftMax - height[left];
56+
} else {
57+
right--;
58+
rightMax = Math.max(rightMax, height[right]);
59+
water += rightMax - height[right];
60+
}
61+
}
62+
return water;
63+
}
64+
}
65+
```
66+
67+
```python
68+
class Solution:
69+
def trap(self, height: list[int]) -> int:
70+
if not height:
71+
return 0
72+
73+
left = 0
74+
right = len(height) - 1
75+
left_max = height[left]
76+
right_max = height[right]
77+
water = 0
78+
79+
while left < right:
80+
if left_max < right_max:
81+
left += 1
82+
left_max = max(left_max, height[left])
83+
water += left_max - height[left]
84+
else:
85+
right -= 1
86+
right_max = max(right_max, height[right])
87+
water += right_max - height[right]
88+
return water
89+
```
90+
91+
## Complexity
92+
93+
- Time complexity: $$O(n)$$
94+
<!-- Add time complexity here, e.g. $$O(n)$$ -->
95+
96+
- Space complexity: $$O(1)$$
97+
<!-- Add space complexity here, e.g. $$O(n)$$ -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int trap(int[] height) {
3+
int left = 0, right = height.length - 1;
4+
int leftMax = height[left], rightMax = height[right];
5+
int water = 0;
6+
7+
while (left < right) {
8+
if (leftMax < rightMax) {
9+
left++;
10+
leftMax = Math.max(leftMax, height[left]);
11+
water += leftMax - height[left];
12+
} else {
13+
right--;
14+
rightMax = Math.max(rightMax, height[right]);
15+
water += rightMax - height[right];
16+
}
17+
}
18+
return water;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def trap(self, height: list[int]) -> int:
3+
if not height:
4+
return 0
5+
6+
left = 0
7+
right = len(height) - 1
8+
left_max = height[left]
9+
right_max = height[right]
10+
water = 0
11+
12+
while left < right:
13+
if left_max < right_max:
14+
left += 1
15+
left_max = max(left_max, height[left])
16+
water += left_max - height[left]
17+
else:
18+
right -= 1
19+
right_max = max(right_max, height[right])
20+
water += right_max - height[right]
21+
return water

0 commit comments

Comments
 (0)