Skip to content

Change in Tries #12279

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
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Welcome to [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python)! Befo

## Contributing


## Contributing

## Rushikesh Kotkar

Contibuted for largest rectangle histogram with best time complexity with great example and explaination.
You can check and learn the algorithm from that

### Contributor

We are delighted that you are considering implementing algorithms and data structures for others! This repository is referenced and used by learners from all over the globe. By being one of our contributors, you agree and confirm that:
Expand Down
120 changes: 120 additions & 0 deletions data_structures/stacks/largest_rectangle_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

"""
Intuition
The problem involves finding the largest rectangle that can be formed in a histogram of different heights. The approach is to iteratively process the histogram's bars while keeping track of the maximum area found so far.

Check failure on line 4 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:4:89: E501 Line too long (220 > 88)

Approach
Initialize a variable maxArea to store the maximum area found, and a stack to keep track of the indices and heights of the histogram bars.

Check failure on line 7 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:7:89: E501 Line too long (138 > 88)

Iterate through the histogram using an enumeration to access both the index and height of each bar.

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 (99 > 88)

For each bar, calculate the width of the potential rectangle by subtracting the starting index (retrieved from the stack) from the current index.

Check failure on line 11 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:11:89: E501 Line too long (145 > 88)

While the stack is not empty and the height of the current bar is less than the height of the bar at the top of the stack, pop elements from the stack to calculate the area of rectangles that can be formed.

Check failure on line 13 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:13:89: E501 Line too long (206 > 88)

Update maxArea with the maximum of its current value and the area calculated in step 4.

Push the current bar's index and height onto the stack to continue processing.

After processing all bars, there may still be bars left in the stack. For each remaining bar in the stack, calculate the area using the height of the bar and the difference between the current index and the index at the top of the stack.

Check failure on line 19 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:19:89: E501 Line too long (237 > 88)

Return maxArea as the result, which represents the largest rectangle area.

Complexity
Time complexity: O(n), where n is the number of bars in the histogram. We process each bar once.

Check failure on line 24 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:24:89: E501 Line too long (96 > 88)
Space complexity: O(n), as the stack can contain up to n elements in the worst case when the bars are in increasing order (monotonic).

Check failure on line 25 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:25:89: E501 Line too long (134 > 88)



Step-by-Step Diagram Creation
Draw the Histogram:

Use vertical bars to represent the heights of the histogram.
Highlight the Largest Rectangle:

Shade or color the area of the largest rectangle.
Example Diagram
plaintext
Copy code
6 | #
| #
5 | # #
| # #
4 | # #
| # #
3 | # # #
| # # #
2 | # # # # # #
| # # # # # #
1 | # # # # # # # #
|_____________________
0 1 2 3 4 5
Highlight the Largest Rectangle
Now, let's highlight the largest rectangle:

The largest rectangle has a height of 6 and spans from index 0 to 3.
plaintext
Copy code
6 | #
| #
5 | # #
| # #
4 | # #
| # #
3 | ██████████
| ██████████
2 | ██████████
| ██████████
1 | # # # # # # # #
|_____________________
0 1 2 3 4 5
Final Summary
The shaded area represents the largest rectangle in the histogram.
Area: 24 (Height = 6, Width = 4)
Indices Covered: 0 to 3



"""

def largestRectangleArea(self, heights: List[int]) -> int:

Check failure on line 80 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:80:5: N802 Function name `largestRectangleArea` should be lowercase

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ARG001)

data_structures/stacks/largest_rectangle_histogram.py:80:26: ARG001 Unused function argument: `self`
# Initialize the maximum area to 0
max_area = 0

# Create a stack to store pairs of (index, height)
stack = []

# Iterate over each bar in the histogram with its index
for in_, ht in enumerate(heights):
# Set the starting index for the rectangle to the current index
st = in_

# While the stack is not empty and the height of the bar at the top of the stack is greater than the current height
while len(stack)>0 and stack[-1][-1] > ht:
# Pop the top element from the stack to get the index and height of that bar
top=stack.pop()
ind=top[0]
height = top[1]

# Calculate the area with the height of the popped bar and the width from the current index to the popped index
max_area = max(max_area, height * (in_ - ind))

# Update the starting index to the index of the popped bar
st = ind

# Push the current height and the starting index onto the stack
stack.append([st, ht])

# After processing all bars, calculate areas for remaining bars in the stack
for i, h in stack:
# Calculate area with the height of the remaining bars and width from their index to the end of the heights list
max_area = max(max_area, h * (len(heights) - i))

# Return the maximum area found
return max_area

heights = [2,4]

largest_Rectangle_Area=largestRectangleArea(heights)

print(largest_Rectangle_Area) ## answer will be 4 for current input
1 change: 1 addition & 0 deletions data_structures/trie/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
longest word)) lookup time making it an optimal approach when space is not an issue.
"""

## This can be changed

class TrieNode:
def __init__(self) -> None:
Expand Down
Loading