From b9b739b93d985bfa7a0aa4f2960f026ef0b6dbf5 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Mon, 2 Oct 2023 19:15:11 -0700 Subject: [PATCH] passes 27/98 cases --- .../84_Largest_Rectangle_in_Histogram.py | 40 +++++++++++++++++++ NeetCodeRoadmap/Stack/tempCodeRunnerFile.py | 6 ++- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 NeetCodeRoadmap/Stack/84_Largest_Rectangle_in_Histogram.py diff --git a/NeetCodeRoadmap/Stack/84_Largest_Rectangle_in_Histogram.py b/NeetCodeRoadmap/Stack/84_Largest_Rectangle_in_Histogram.py new file mode 100644 index 0000000..318499d --- /dev/null +++ b/NeetCodeRoadmap/Stack/84_Largest_Rectangle_in_Histogram.py @@ -0,0 +1,40 @@ +class Solution(object): + def largestRectangleArea(self, heights): + """ + :type heights: List[int] + :rtype: int + """ + rect = [] + h = len(heights) + + if len(heights) <= 2: + if len(heights) == 1: + return heights[0] + else: + if heights[0] == 0 or heights[1] == 0: + return max(heights[0], heights[1]) + low = min(heights[0], heights[1]) + return low*2 + + for x in range(h): + count = 1 + for y in range(x+1, h): + if heights[x] == min(heights): + rect.append(heights[x]*len(heights)) + elif heights[x] > heights[y]: + # print(f'{heights[x]} * {count} = {heights[x]*count}') + rect.append(heights[x]*count) + else: + count += 1 + + if rect: + return max(rect) + else: + return 0 +obj = Solution() +# print(obj.largestRectangleArea([2,1,5,6,2,3])) # 10 +# print(obj.largestRectangleArea([2,4])) # 4 +# print(obj.largestRectangleArea([0,9])) # 9 +# print(obj.largestRectangleArea([0,0,0])) # 0 +# print(obj.largestRectangleArea([2,1,2])) # 3 +print(obj.largestRectangleArea([1,2,2]) # 4 \ No newline at end of file diff --git a/NeetCodeRoadmap/Stack/tempCodeRunnerFile.py b/NeetCodeRoadmap/Stack/tempCodeRunnerFile.py index 4459339..5fa34dc 100644 --- a/NeetCodeRoadmap/Stack/tempCodeRunnerFile.py +++ b/NeetCodeRoadmap/Stack/tempCodeRunnerFile.py @@ -1,2 +1,4 @@ -print(obj.carFleet(10,[3],[3])) # 1 -print(obj.carFleet(100,[0,2,4],[4,2,1])) # 1 \ No newline at end of file +print(obj.largestRectangleArea([2,1,5,6,2,3])) # 10 +print(obj.largestRectangleArea([2,4])) # 4 +print(obj.largestRectangleArea([0,9])) # 9 +print(obj.largestRectangleArea([0,0,0])) # 9 \ No newline at end of file