Skip to content
Closed
Changes from all 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
55 changes: 55 additions & 0 deletions data_structures/stacks/largest_rectangle_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Author: Ansh Dulewale
GitHub: github.com/ansh-dulewale

Explanation: http://www.codingdrills.com/tutorial/stack-data-structure/largest-rectangle-in-histogram

THESE ARE THE ALGORITHM'S RULES:
RULE 1: Scan `heights` array in left to right direction
if the height at this index is greater than the height at the top position of `stack`

Check failure on line 9 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/stacks/largest_rectangle_histogram.py:9:89: E501 Line too long (93 > 88)
we push the index at `stack`.

RULE 2: If the `height` at the current `index` is smaller than the `height` at the top of the `stack`

Check failure on line 12 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/stacks/largest_rectangle_histogram.py:12:89: E501 Line too long (101 > 88)
process the stack now. Pop top `index` from `stack`.
The `height` of the rectangle = `height` at popped `index`.
Calculate `width` of the rectangle:
if the `stack` is not empty.
Then the `width` extends from `begin` of the `histogram` till current `index`.
If the stack is not empty, the width stretches from a current index to an index that immediately comes after new stack top

Check failure on line 18 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/stacks/largest_rectangle_histogram.py:18:89: E501 Line too long (130 > 88)

RULE 3: area = height width if it is greater than max_area so far
update max_area.

RULE 4: Repeat Rules 2 and 3 until the current height is no longer less than the height at the top index of the `stack`.

Check failure on line 23 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/stacks/largest_rectangle_histogram.py:23:89: E501 Line too long (120 > 88)
Push the current index onto the `stack`.

RULE 5: After the entire `heights` array has been traversed, the maximum area found, stored in `max_area`,

Check failure on line 26 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/stacks/largest_rectangle_histogram.py:26:89: E501 Line too long (106 > 88)
represents the area of the largest rectangle in the histogram.

NOTE: It only works with whole numbers.
"""

__author__ = "Ansh Dulewale"


def largestRectangleArea(heights):

Check failure on line 35 in data_structures/stacks/largest_rectangle_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/stacks/largest_rectangle_histogram.py:35:5: N802 Function name `largestRectangleArea` should be lowercase
stack = []
max_area = 0
heights.append(0) # Add a zero height to pop all remaining heights in stack.

for i in range(len(heights)):
while stack and heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i if not stack else i - stack[-1] - 1
max_area = max(max_area, h * w)
stack.append(i)

heights.pop() # Restore the original heights array.
return max_area


# Get input from the user
user_input = input()
heights = list(map(int, user_input.split()))

print(largestRectangleArea(heights))
Loading