Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add problem resolved using monotonic stack #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Stack/Python/largest-rectangle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return _the area of the largest rectangle in the histogram_.


Example 1:

![example1](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)


```
Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
```

Example 2:

![example2](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)


```
Input: heights = [2,4]
Output: 4
```


Constraints:

- `1 <= heights.length <= 105`
- `0 <= heights[i] <= 104`
15 changes: 15 additions & 0 deletions Stack/Python/largest-rectangle/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
ret = 0
mono_stack = []
heights.append(0)
for i, v in enumerate(heights):
while mono_stack and heights[mono_stack[-1]] > v:
height = heights[mono_stack.pop()]
if mono_stack:
length = i - mono_stack[-1]-1
else:
length = i
ret = max(ret, height * length)
mono_stack.append(i)
return ret
28 changes: 28 additions & 0 deletions Stack/Python/trapping-rain-water/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.



Example 1:

![example1](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)


```
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) 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.
```

Example 2:

```
Input: height = [4,2,0,3,2,5]
Output: 9
```


Constraints:

- `n == height.length`
- `1 <= n <= 2 * 104`
- `0 <= height[i] <= 105`
14 changes: 14 additions & 0 deletions Stack/Python/trapping-rain-water/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def trap(self, heights: List[int]) -> int:
stack = [] # a decreasing stack
total = 0
for i, v in enumerate(heights):
while stack and heights[stack[-1]] < v:
popped_idx = stack.pop()
if not stack:
break
height = min(heights[stack[-1]], v) - heights[popped_idx]
length = i - stack[-1] - 1
total += height * length
stack.append(i)
return total