Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- If we consider each row as a base and then find the maximum reactangle in histogram then you will get the maximum
- rectange from the binary matrix.
- */
- int largest_rectangle(int arr[],int n) {
- stack<pair<int,int>> st;
- st.push({0,-1});
- int ans=INT_MIN;
- for(int i=1;i<n;i++){
- while(st.size() && arr[st.top().first]>=arr[i]){
- ans=max((i-st.top().second-1)*arr[st.top().first],ans);
- st.pop();
- }
- if(st.size()==0){
- st.push({i,-1});
- }
- else{
- st.push({i,st.top().first});
- }
- }
- while(st.size()){
- ans=max(ans,(n-st.top().second-1)*arr[st.top().first]);
- st.pop();
- }
- return ans;
- }
- class Solution{
- public:
- int maxArea(int arr[MAX][MAX], int n, int m) {
- int ans=INT_MIN;
- for(int i=0;i<n;i++){
- if(i!=0){
- for(int j=0;j<m;j++)
- if(arr[i][j]!=0) arr[i][j]+=arr[i-1][j];
- else arr[i][j]=0;
- }
- ans=max(ans,largest_rectangle(arr[i],m));
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement