柱状图中最大的矩形
解题思路:分冶法
class Solution { public int largestRectangleArea(int[] heights) { int len = heights.length; if(len==0){ return 0; } int min = minArray(heights, 0, len); int areaMax = heights[min]*len; return Math.max(areaMax, Math.max(largestRectangleArea2(heights,0,min),largestRectangleArea2(heights,min+1,len))); } public int largestRectangleArea2(int[] heights,int start,int end) { if(start==end) { return 0; } int min = minArray(heights, start, end); int areaMax = heights[min]*(end - start); return Math.max(areaMax, Math.max(largestRectangleArea2(heights,start,min),largestRectangleArea2(heights,min+1,end))); } public int minArray(int[] heights,int start, int end) { int min = heights[start]; int k = start; for(int i=start+1;i<end;i++) { if(heights[i]<min) { min = heights[i]; k = i; } } return k; } }
参考文献: