求直方圖中的最大矩形面積:
例如給定直方圖{2,3,1,2,4,2}
則直方圖中最大矩形面積為x=(3,6),|x|=3,y=2,max面積=6
思考:利用枚舉法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/*當前位置往前進行枚舉法*/ publicclass solution{ static int histogrammaxarea( int []a ){ int maxs =a [ 0 ]; for ( int i = 0 ;i <a .length;i ++){ //直方圖中依次向后枚舉 int min =a [i ]; //記錄當前條圖及之前最小值 int m = 0 ; //記錄底部邊長 for ( int j =i ;j >= 0 ;j --){ //依次向前取最大矩形 m++; if ( a[ j]< min){ min= a[ j]; } int s =m *min ; //矩形面積計算 if ( s> maxs){ maxs= s; } } } return maxs ; } public static void main(string args[]){ int a []={ 2 , 1 , 1 , 2 }; int maxarea =histogrammaxarea( a); system. out.print(maxarea ); } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/y999666/article/details/50786041