From fad0a6f8971b80b59065d8dafbd93ef0c8402ada Mon Sep 17 00:00:00 2001 From: Ansh Dulewale <120741334+ansh-dulewale@users.noreply.github.com> Date: Mon, 28 Oct 2024 08:33:11 +0530 Subject: [PATCH] Create largest_rectangle_histogram.py --- .../stacks/largest_rectangle_histogram.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 data_structures/stacks/largest_rectangle_histogram.py diff --git a/data_structures/stacks/largest_rectangle_histogram.py b/data_structures/stacks/largest_rectangle_histogram.py new file mode 100644 index 000000000000..a2fa3cbe8839 --- /dev/null +++ b/data_structures/stacks/largest_rectangle_histogram.py @@ -0,0 +1,20 @@ +def largest_Rectangle_Area(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 of array. + return max_area + +# Get input from the user +user_input = input() +heights = list(map(int, user_input.split())) + +print(largestRectangleArea(heights))