Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 88b9733

Browse files
Merge pull request youngyangyang04#604 from lichun-chen/master
更新 0042接雨水 动态规划和单调栈Python3版本。更新0084柱状图中最大的矩形 动态规划和单调栈Python3版本
2 parents 7f499c7 + 368fa2b commit 88b9733

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

problems/0042.接雨水.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,44 @@ class Solution:
388388
res += res1
389389
return res
390390
```
391+
动态规划
392+
```python3
393+
class Solution:
394+
def trap(self, height: List[int]) -> int:
395+
leftheight, rightheight = [0]*len(height), [0]*len(height)
396+
397+
leftheight[0]=height[0]
398+
for i in range(1,len(height)):
399+
leftheight[i]=max(leftheight[i-1],height[i])
400+
rightheight[-1]=height[-1]
401+
for i in range(len(height)-2,-1,-1):
402+
rightheight[i]=max(rightheight[i+1],height[i])
403+
404+
result = 0
405+
for i in range(0,len(height)):
406+
summ = min(leftheight[i],rightheight[i])-height[i]
407+
result += summ
408+
return result
409+
```
410+
单调栈
411+
```python3
412+
class Solution:
413+
def trap(self, height: List[int]) -> int:
414+
st =[0]
415+
result = 0
416+
for i in range(1,len(height)):
417+
while st!=[] and height[i]>height[st[-1]]:
418+
midh = height[st[-1]]
419+
st.pop()
420+
if st!=[]:
421+
hright = height[i]
422+
hleft = height[st[-1]]
423+
h = min(hright,hleft)-midh
424+
w = i-st[-1]-1
425+
result+=h*w
426+
st.append(i)
427+
return result
428+
```
391429

392430
Go:
393431

problems/0084.柱状图中最大的矩形.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,57 @@ public:
191191

192192
这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节!
193193

194+
## 其他语言版本
195+
196+
Java:
197+
198+
Python:
199+
200+
动态规划
201+
```python3
202+
class Solution:
203+
def largestRectangleArea(self, heights: List[int]) -> int:
204+
result = 0
205+
minleftindex, minrightindex = [0]*len(heights), [0]*len(heights)
206+
207+
minleftindex[0]=-1
208+
for i in range(1,len(heights)):
209+
t = i-1
210+
while t>=0 and heights[t]>=heights[i]: t=minleftindex[t]
211+
minleftindex[i]=t
212+
213+
minrightindex[-1]=len(heights)
214+
for i in range(len(heights)-2,-1,-1):
215+
t=i+1
216+
while t<len(heights) and heights[t]>=heights[i]: t=minrightindex[t]
217+
minrightindex[i]=t
218+
219+
for i in range(0,len(heights)):
220+
left = minleftindex[i]
221+
right = minrightindex[i]
222+
summ = (right-left-1)*heights[i]
223+
result = max(result,summ)
224+
return result
225+
```
226+
单调栈 版本二
227+
```python3
228+
class Solution:
229+
def largestRectangleArea(self, heights: List[int]) -> int:
230+
heights.insert(0,0) # 数组头部加入元素0
231+
heights.append(0) # 数组尾部加入元素0
232+
st = [0]
233+
result = 0
234+
for i in range(1,len(heights)):
235+
while st!=[] and heights[i]<heights[st[-1]]:
236+
midh = heights[st[-1]]
237+
st.pop()
238+
if st!=[]:
239+
minrightindex = i
240+
minleftindex = st[-1]
241+
summ = (minrightindex-minleftindex-1)*midh
242+
result = max(summ,result)
243+
st.append(i)
244+
return result
245+
```
246+
194247
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

0 commit comments

Comments
 (0)