1.3
This commit is contained in:
1 parent
ff8c92b6b5
commit
864fc9e663
5 files changed
+168
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package com.hong.www;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class A84 {
|
||||
public int largestRectangleArea(int[] heights) {
|
||||
if(heights.length==1){
|
||||
return heights[0];
|
||||
}
|
||||
int currentMax = 0;
|
||||
int finalMax = 0;
|
||||
ArrayList<Integer> maxs=new ArrayList<>();
|
||||
for(int i = 0; i < heights.length; i++) {
|
||||
if(heights[i]>currentMax){
|
||||
maxs.clear();
|
||||
maxs.add(i);
|
||||
currentMax=heights[i];
|
||||
}else if(heights[i]==currentMax){
|
||||
maxs.add(i);
|
||||
}
|
||||
}
|
||||
for(int x:maxs){
|
||||
int currectMin=Integer.MAX_VALUE;
|
||||
//int max=0;
|
||||
for(int i=x-1;i>=0;i--){
|
||||
if(heights[i]<=currectMin){
|
||||
currectMin=heights[i];
|
||||
if(currectMin*(x-i+1)>finalMax){
|
||||
finalMax=currectMin*(x-i+1);
|
||||
}
|
||||
}else{
|
||||
if(currectMin*(x-i+1)>finalMax){
|
||||
finalMax=currectMin*(x-i+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
currectMin=Integer.MAX_VALUE;
|
||||
for(int i=x+1;i<heights.length;i++){
|
||||
if(heights[i]<=currectMin){
|
||||
currectMin=heights[i];
|
||||
if(currectMin*(i-x+1)>finalMax){
|
||||
finalMax=currectMin*(i-x+1);
|
||||
}
|
||||
}else{
|
||||
if(currectMin*(i-x+1)>finalMax){
|
||||
finalMax=currectMin*(i-x+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(finalMax==0){
|
||||
return currentMax;
|
||||
}
|
||||
return finalMax;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
A84 a84=new A84();
|
||||
int[] heights={4,2};
|
||||
System.out.println(a84.largestRectangleArea(heights));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user