imashutosh51

Maximum ractangular area in a histogram

Jul 23rd, 2022 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. '''
  2. Logic:
  3. M-1: T.C : O(n) S.C : O(n) : 3 pass
  4. Basically we will find the left first min and right first min using stack and then again traversing the array
  5. and distance between left min to right min multiplied by current element height and update the maximum everytime.
  6.  
  7. M-2: T.C : O(n) S.C : O(n) : 1 pass
  8. Suppose we are finding the last min using stack of pair where stack first element will be index of current element
  9. and second element will be the index of left minimum element(-1 if there is no such element exists)
  10. Steps:
  11. push first element index with -1 because no element before it and start iterating from first index
  12. Case 1: suppose current top element of stack is smaller than current ith element means for ith element,left minimum is top
  13. element index so push current element in stack as {i,st.top().first}.So if any element is in stack that means it's left
  14. minimum index will also be there as second element.
  15.  
  16. Case 2: Suppose current top element of stack is greater or equal to current element then first right minimum of top
  17. element will be the current element and stack top element second element denotes the left minimum element index so
  18. now we can calculate the area of histogram of arr[i] height by (right-left-1)*top_element_height so find it and update
  19. every time.
  20.  
  21. After doing for whole array,find all histogram area which is present in stack as their next minimum is not present in array
  22. so consider n as next right min element and update the answer also.
  23. '''
  24.  
  25. class Solution:
  26.     def largestRectangleArea(self, arr: List[int]) -> int:
  27.         st = []   # [height, index]
  28.         ans = 0
  29.         n = len(arr)
  30.  
  31.         for i in range(n):
  32.             while st and arr[i] < st[-1][0]:
  33.                 h, idx = st.pop()
  34.                 left = st[-1][1] if st else -1
  35.                 width = i - left - 1
  36.                 ans = max(ans, h * width)
  37.             st.append([arr[i], i])
  38.  
  39.         while st:
  40.             h, idx = st.pop()
  41.             left = st[-1][1] if st else -1
  42.             width = n - left - 1
  43.             ans = max(ans, h * width)
  44.  
  45.         return ans
  46.  
  47. #Just another design
  48. class Solution:
  49.     def largestRectangleArea(self, heights: List[int]) -> int:
  50.         ans=0
  51.         st=[]
  52.         st.append(-1)
  53.         for i in range(len(heights)):
  54.             while len(st)>1 and heights[st[-1]]>heights[i]:
  55.                 temp=st.pop()
  56.                 ans=max(ans,(i-st[-1]-1)*heights[temp])
  57.             st.append(i)
  58.         n=len(heights)
  59.         while len(st)>1:
  60.             temp=st.pop()
  61.             ans=max(ans,(n-st[-1]-1)*heights[temp])
  62.         return ans
Advertisement
Add Comment
Please, Sign In to add comment