You are viewing a single comment's thread. Return to all comments →
no it will give 10 .. try this import java.util.*; // @author satyendra public class areaHistogram { static int area(int[] a) { int i,area=0; int maxArea=0; Stack<Integer> st=new Stack<>(); for(i=0;i<a.length;) { if(st.isEmpty()||a[st.peek()]<=a[i]) { st.push(i++); } else { int x=st.pop(); if(st.isEmpty()) { area=a[x]*i; } else { area=a[x]*(i-st.peek()-1); } if(area>maxArea) maxArea=area; } } while(!st.isEmpty()) { int x=st.pop(); if(st.isEmpty()) { area=a[x]*i; } else { area=a[x]*(i-st.peek()-1); } if(area>maxArea) maxArea=area; } return maxArea; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); System.out.println("max area="+area(a)); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Rectangle
You are viewing a single comment's thread. Return to all comments →
}