Advertisement
imashutosh51

Maximum ractangular area in a histogram

Jul 23rd, 2022 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.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.  
  26. //M-1
  27. #include <bits/stdc++.h>
  28. using namespace std;
  29. class Solution
  30. {
  31.     public:
  32.     long long getMaxArea(long long arr[], int n)
  33.     {
  34.         vector <pair<long long int,long long int>> left_min,right_min;
  35.         stack<pair<long long int,long long int>> st;
  36.         for(int i=0;i<n;i++){
  37.             if(st.empty()){
  38.                 left_min.push_back(make_pair(-1,arr[i]));
  39.                 st.push(make_pair(i,arr[i]));
  40.                
  41.             }
  42.             else{
  43.                 while(!st.empty() && st.top().second>=arr[i])
  44.                    st.pop();
  45.                 if(!st.empty())
  46.                     left_min.push_back(st.top());
  47.                 else
  48.                    left_min.push_back(make_pair(-1,arr[i]));
  49.                 st.push(make_pair(i,arr[i]));
  50.                 }
  51.             }
  52.         while(!st.empty())
  53.            st.pop();
  54.          for(int i=n-1;i>=0;i--){
  55.             if(st.empty()){
  56.                 right_min.push_back(make_pair(n,arr[i]));
  57.                 st.push(make_pair(i,arr[i]));
  58.                
  59.             }
  60.             else{
  61.                 while(!st.empty() && st.top().second>=arr[i])
  62.                    st.pop();
  63.                 if(!st.empty())
  64.                     right_min.push_back(st.top());
  65.                 else
  66.                    right_min.push_back(make_pair(n,arr[i]));
  67.                 st.push(make_pair(i,arr[i]));
  68.                 }
  69.             }
  70.             reverse(right_min.begin(),right_min.end());
  71.             long long int area=INT_MIN;
  72.             for(int i=0;i<n;i++){
  73.                 if(area<(arr[i]+abs(left_min[i].first+1-i)*arr[i]+abs(right_min[i].first-i-1)*arr[i]))
  74.                 area=(arr[i]+abs(left_min[i].first+1-i)*arr[i]+abs(right_min[i].first-i-1)*arr[i]);
  75.             }
  76.            return area;
  77.     }
  78. };
  79.  
  80. int main()
  81.  {
  82.     long long t;
  83.  
  84.     cin>>t;
  85.     while(t--)
  86.     {
  87.         int n;
  88.         cin>>n;
  89.        
  90.         long long arr[n];
  91.         for(int i=0;i<n;i++)
  92.             cin>>arr[i];
  93.         Solution ob;
  94.         cout<<ob.getMaxArea(arr, n)<<endl;
  95.    
  96.     }
  97.     return 0;
  98. }
  99.  
  100. //M-2
  101. class Solution {
  102. long long int _max(long long int a,long long int b){
  103.     if(a>b) return a;
  104.     return b;
  105. }
  106. class Solution
  107. {
  108.     public:
  109.     //Function to find largest rectangular area possible in a given histogram.
  110.     long long getMaxArea(long long arr[], int n)
  111.     {
  112.         stack<pair<int,int>> st;
  113.         long long int ans=arr[0];
  114.         st.push({0,-1});
  115.         for(int i=1;i<n;i++){
  116.             if(arr[i]>=arr[st.top().first]) st.push(make_pair(i,st.top().first));
  117.             else{
  118.                 while(st.size() && arr[i]<arr[st.top().first]){
  119.                     pair<int,int> temp=st.top();
  120.                     st.pop();
  121.                     ans=_max(ans,(i-temp.second-1)*arr[temp.first]);
  122.                     }
  123.                 if(st.size())st.push(make_pair(i,st.top().first));
  124.                 else st.push(make_pair(i,-1));
  125.             }
  126.         }
  127.         while(st.size()){
  128.             ans=_max(ans,(n-st.top().second-1)*arr[st.top().first]);
  129.             st.pop();
  130.         }
  131.         return ans;
  132.     }
  133. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement