Skip to content

Commit 214d6f4

Browse files
authored
Update 0042-接雨水.py
1 parent 74d6832 commit 214d6f4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

0042.接雨水/0042-接雨水.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,37 @@ def trap(self, height: List[int]) -> int:
4040
# 链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode-solution-tuvc/
4141
# 来源:力扣(LeetCode)
4242
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
43+
44+
# 方法三:双指针
45+
# 动态规划的做法中,需要维护两个数组 leftMax 和 rightMax,因此空间复杂度是 O(n)O(n)O(n)。是否可以将空间复杂度降到 O(1)O(1)O(1)?
46+
# 注意到下标 i 处能接的雨水量由 leftMax[i] 和 rightMax[i] 中的最小值决定。由于数组 leftMax 是从左往右计算,数组 rightMax 是从右往左计算,因此可以使用双指针和两个变量代替两个数组。
47+
# 维护两个指针 left 和 right,以及两个变量 leftMax 和 rightMax,初始时 left=0,right=n−1,leftMax=0,rightMax=0。
48+
# 指针 left 只会向右移动,指针 right 只会向左移动,在移动指针的过程中维护两个变量 leftMax 和 rightMax 的值。
49+
50+
# 当两个指针没有相遇时,进行如下操作:
51+
# 使用 height[left] 和 height[right] 的值更新 leftMax 和 rightMax 的值;
52+
# 如果 height[left]<height[right],则必有 leftMax<rightMax,下标 left 处能接的雨水量等于 leftMax−height[left],将下标 left 处能接的雨水量加到能接的雨水总量,然后将 left 加 1(即向右移动一位);
53+
# 如果 height[left]≥height[right],则必有 leftMax≥rightMax,下标 right 处能接的雨水量等于 rightMax−height[right],将下标 right 处能接的雨水量加到能接的雨水总量,然后将 right 减 1(即向左移动一位)。
54+
# 当两个指针相遇时,即可得到能接的雨水总量。
55+
56+
class Solution:
57+
def trap(self, height: List[int]) -> int:
58+
ans = 0
59+
left, right = 0, len(height) - 1
60+
leftMax = rightMax = 0
61+
62+
while left < right:
63+
leftMax = max(leftMax, height[left])
64+
rightMax = max(rightMax, height[right])
65+
if height[left] < height[right]:
66+
ans += leftMax - height[left]
67+
left += 1
68+
else:
69+
ans += rightMax - height[right]
70+
right -= 1
71+
72+
return ans
73+
# 作者:力扣官方题解
74+
# 链接:https://leetcode.cn/problems/trapping-rain-water/solutions/692342/jie-yu-shui-by-leetcode-solution-tuvc/
75+
# 来源:力扣(LeetCode)
76+
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 commit comments

Comments
 (0)