- 单调栈的最大宽度:
- 1 2 6 3 5 7
- 1 2 6
- 3 5 7
- max=10
public static void getMaxWidth(int[] arr){
Stack<Integer> stack=new Stack<Integer>();
int max=0;
int len=1;
for(int i=0;i<arr.length;i++){
if((!stack.isEmpty()&&stack.peek()<arr[i])||stack.isEmpty()){
stack.push(arr[i]);
}
if(!stack.isEmpty()&&stack.peek()>arr[i]||i==arr.length-1){
while(!stack.isEmpty()){
int tmp=stack.pop()*len;
max=tmp>max?tmp:max;
len++;
}
len=1;
stack.push(arr[i]);
}
}
System.out.println(max);
}