From 7520943c61022245d8cb83efae0236837b1f5d3b Mon Sep 17 00:00:00 2001 From: frontend-quicksell Date: Thu, 24 Oct 2024 21:02:43 +0530 Subject: [PATCH 1/5] I have added largest rectangle histogram in the following : data_stracture > stacks >largest_rectangle_histogram --- CONTRIBUTING.md | 8 ++ .../stacks/largest_rectangle_histogram,py | 118 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 data_structures/stacks/largest_rectangle_histogram,py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3df39f95b784..038b4193f349 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: diff --git a/data_structures/stacks/largest_rectangle_histogram,py b/data_structures/stacks/largest_rectangle_histogram,py new file mode 100644 index 000000000000..325e0593ba5e --- /dev/null +++ b/data_structures/stacks/largest_rectangle_histogram,py @@ -0,0 +1,118 @@ + +""" + 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. + +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. + +Iterate through the histogram using an enumeration to access both the index and height of each bar. + +For each bar, calculate the width of the potential rectangle by subtracting the starting index (retrieved from the stack) from the current index. + +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. + +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. + +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. +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). + + + +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: + # 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 i, h in enumerate(heights): + # Set the starting index for the rectangle to the current index + start = i + + # 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 stack and stack[-1][-1] > h: + # Pop the top element from the stack to get the index and height of that bar + index, ph = stack.pop() + + # 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, ph * (i - index)) + + # Update the starting index to the index of the popped bar + start = index + + # Push the current height and the starting index onto the stack + stack.append([start, h]) + + # 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 \ No newline at end of file From 406cb71d477a69c8260192492805f019883a67c1 Mon Sep 17 00:00:00 2001 From: frontend-quicksell Date: Thu, 24 Oct 2024 21:09:27 +0530 Subject: [PATCH 2/5] I have added largest rectangle histogram in the following : data_stracture > stacks >largest_rectangle_histogram --- ...gest_rectangle_histogram,py => largest_rectangle_histogram.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/stacks/{largest_rectangle_histogram,py => largest_rectangle_histogram.py} (100%) diff --git a/data_structures/stacks/largest_rectangle_histogram,py b/data_structures/stacks/largest_rectangle_histogram.py similarity index 100% rename from data_structures/stacks/largest_rectangle_histogram,py rename to data_structures/stacks/largest_rectangle_histogram.py From 77ee22dca0161b29d0cc823369c64dd8d820f4de Mon Sep 17 00:00:00 2001 From: frontend-quicksell Date: Thu, 24 Oct 2024 21:09:59 +0530 Subject: [PATCH 3/5] I have added largest rectangle histogram in the following : data_stracture > stacks >largest_rectangle_histogram --- data_structures/stacks/largest_rectangle_histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/largest_rectangle_histogram.py b/data_structures/stacks/largest_rectangle_histogram.py index 325e0593ba5e..51a436400dd4 100644 --- a/data_structures/stacks/largest_rectangle_histogram.py +++ b/data_structures/stacks/largest_rectangle_histogram.py @@ -1,6 +1,6 @@ """ - Intuition + 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. Approach From e5e758ed16a144ce18262e3fe0795f90184628f5 Mon Sep 17 00:00:00 2001 From: frontend-quicksell Date: Thu, 24 Oct 2024 21:43:54 +0530 Subject: [PATCH 4/5] I have added largest rectangle histogram in the following : data_stracture > stacks >largest_rectangle_histogram --- .../stacks/largest_rectangle_histogram.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/data_structures/stacks/largest_rectangle_histogram.py b/data_structures/stacks/largest_rectangle_histogram.py index 51a436400dd4..a6e2a6967c90 100644 --- a/data_structures/stacks/largest_rectangle_histogram.py +++ b/data_structures/stacks/largest_rectangle_histogram.py @@ -85,23 +85,25 @@ def largestRectangleArea(self, heights: List[int]) -> int: stack = [] # Iterate over each bar in the histogram with its index - for i, h in enumerate(heights): + for in_, ht in enumerate(heights): # Set the starting index for the rectangle to the current index - start = i + 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 stack and stack[-1][-1] > h: + 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 - index, ph = stack.pop() + 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, ph * (i - index)) + max_area = max(max_area, height * (in_ - ind)) # Update the starting index to the index of the popped bar - start = index + st = ind # Push the current height and the starting index onto the stack - stack.append([start, h]) + stack.append([st, ht]) # After processing all bars, calculate areas for remaining bars in the stack for i, h in stack: From a138031d833564204bf05422ff00d625a943424d Mon Sep 17 00:00:00 2001 From: frontend-quicksell Date: Fri, 25 Oct 2024 18:07:44 +0530 Subject: [PATCH 5/5] changed in Trie --- data_structures/trie/trie.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/trie/trie.py b/data_structures/trie/trie.py index 46b93a499d14..75aa0bc8849f 100644 --- a/data_structures/trie/trie.py +++ b/data_structures/trie/trie.py @@ -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: