Update A84.java
This commit is contained in:
1 parent
864fc9e663
commit
6a0ea68a2f
1 file changed
+21
-46
+21
-46
@@ -4,58 +4,33 @@ import java.util.*;
|
|||||||
|
|
||||||
public class A84 {
|
public class A84 {
|
||||||
public int largestRectangleArea(int[] heights) {
|
public int largestRectangleArea(int[] heights) {
|
||||||
if(heights.length==1){
|
LinkedList<Integer> stack = new LinkedList<>();
|
||||||
return heights[0];
|
int finalcount=0;
|
||||||
}
|
int[] newheights=new int[heights.length+2];
|
||||||
int currentMax = 0;
|
newheights[0] = 0;
|
||||||
int finalMax = 0;
|
newheights[newheights.length - 1] = 0;
|
||||||
ArrayList<Integer> maxs=new ArrayList<>();
|
for (int i = 0; i < heights.length; i++) {
|
||||||
for(int i = 0; i < heights.length; i++) {
|
newheights[i + 1] = heights[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);
|
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < newheights.length; i++) {
|
||||||
|
if (stack.size() == 0) {
|
||||||
|
stack.add(i);
|
||||||
}else{
|
}else{
|
||||||
if(currectMin*(x-i+1)>finalMax){
|
while(!stack.isEmpty()&&newheights[i]<newheights[stack.get(stack.size()-1)]){
|
||||||
finalMax=currectMin*(x-i+1);
|
if(finalcount<(i-stack.get(stack.size()-2)-1)*newheights[stack.get(stack.size()-1)]){
|
||||||
|
finalcount=(i-stack.get(stack.size()-2)-1)*newheights[stack.get(stack.size()-1)];
|
||||||
|
}
|
||||||
|
stack.remove(stack.size()-1);
|
||||||
|
}
|
||||||
|
stack.add(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return finalcount;
|
||||||
}
|
}
|
||||||
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) {
|
public static void main(String[] args) {
|
||||||
A84 a84=new A84();
|
A84 a84 = new A84();
|
||||||
int[] heights={4,2};
|
int[] heights = { 4, 2,4 };
|
||||||
System.out.println(a84.largestRectangleArea(heights));
|
System.out.println(a84.largestRectangleArea(heights));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user