From 52cf4e0f18373b76a290ee2a923a45617ba7fad3 Mon Sep 17 00:00:00 2001 From: lichun-chen <81766312+lichun-chen@users.noreply.github.com> Date: Fri, 13 Aug 2021 21:42:03 -0500 Subject: [PATCH 1/2] =?UTF-8?q?Update=200042=E6=8E=A5=E9=9B=A8=E6=B0=B4.md?= =?UTF-8?q?=20=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E5=92=8C=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E6=A0=88Python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0042.接雨水.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 235776a0..a2cb2345 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -388,6 +388,44 @@ class Solution: res += res1 return res ``` +动态规划 +```python3 +class Solution: + def trap(self, height: List[int]) -> int: + leftheight, rightheight = [0]*len(height), [0]*len(height) + + leftheight[0]=height[0] + for i in range(1,len(height)): + leftheight[i]=max(leftheight[i-1],height[i]) + rightheight[-1]=height[-1] + for i in range(len(height)-2,-1,-1): + rightheight[i]=max(rightheight[i+1],height[i]) + + result = 0 + for i in range(0,len(height)): + summ = min(leftheight[i],rightheight[i])-height[i] + result += summ + return result +``` +单调栈 +```python3 +class Solution: + def trap(self, height: List[int]) -> int: + st =[0] + result = 0 + for i in range(1,len(height)): + while st!=[] and height[i]>height[st[-1]]: + midh = height[st[-1]] + st.pop() + if st!=[]: + hright = height[i] + hleft = height[st[-1]] + h = min(hright,hleft)-midh + w = i-st[-1]-1 + result+=h*w + st.append(i) + return result +``` Go: From 368fa2b761aaf7bee03d6796dd62dd5f157811e7 Mon Sep 17 00:00:00 2001 From: lichun-chen <81766312+lichun-chen@users.noreply.github.com> Date: Fri, 13 Aug 2021 21:49:17 -0500 Subject: [PATCH 2/2] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md=20?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E5=92=8C=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E6=A0=88=20Python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0084.柱状图中最大的矩形.md | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index f0f58c0f..941888db 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -191,4 +191,57 @@ public: 这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节! +## 其他语言版本 + +Java: + +Python: + +动态规划 +```python3 +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: + result = 0 + minleftindex, minrightindex = [0]*len(heights), [0]*len(heights) + + minleftindex[0]=-1 + for i in range(1,len(heights)): + t = i-1 + while t>=0 and heights[t]>=heights[i]: t=minleftindex[t] + minleftindex[i]=t + + minrightindex[-1]=len(heights) + for i in range(len(heights)-2,-1,-1): + t=i+1 + while t=heights[i]: t=minrightindex[t] + minrightindex[i]=t + + for i in range(0,len(heights)): + left = minleftindex[i] + right = minrightindex[i] + summ = (right-left-1)*heights[i] + result = max(result,summ) + return result +``` +单调栈 版本二 +```python3 +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: + heights.insert(0,0) # 数组头部加入元素0 + heights.append(0) # 数组尾部加入元素0 + st = [0] + result = 0 + for i in range(1,len(heights)): + while st!=[] and heights[i]