-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Create largest_rectangle_histogram.py #12305
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
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` | ||
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 | ||
|
||
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`. | ||
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`, | ||
represents the area of the largest rectangle in the histogram. | ||
|
||
NOTE: It only works with whole numbers. | ||
""" | ||
|
||
__author__ = "Ansh Dulewale" | ||
|
||
|
||
def largestRectangleArea(heights): | ||
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)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.