public int solution(int[] heights) { int n = heights.length; if(n==0) return 0; int[] left = new int[n]; int[] right = new int[n]; int res = 0; Stack<Integer> s = new Stack<Integer>(); for(int i = 0 ;i<n;i++){ for(;!s.isEmpty()&&heights[i]<=heights[s.peek()];s.pop()); left[i] = s.isEmpty()?0:s.peek()+1; s.push(i); } s.clear(); for(int i = n-1;i>=0;i--){ for(;!s.isEmpty()&&heights[i]<=heights[s.peek()];s.pop()); right[i] = s.isEmpty()?n-1:s.peek()-1; s.push(i); } for(int i = 0;i<n;i++){ res = Math.max(res,(right[i]-left[i]+1)*heights[i]); } return res; }