2024/9/17 Hot100 Stack 昨日栈
This commit is contained in:
parent
e3896ccc54
commit
8e4d8d1ddd
|
@ -0,0 +1,36 @@
|
||||||
|
package com.cool.hot100.stack;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/09/18/14:20
|
||||||
|
* DayNumber 1
|
||||||
|
* Hard 3
|
||||||
|
* Level ?
|
||||||
|
*/
|
||||||
|
public class Num84 {
|
||||||
|
|
||||||
|
public int largestRectangleArea(int[] heights) {
|
||||||
|
Deque<Integer> deque=new LinkedList<>();
|
||||||
|
int res=0;
|
||||||
|
deque.offerLast(0);
|
||||||
|
int[] newHeights=new int[heights.length+2];
|
||||||
|
System.arraycopy(heights, 0, newHeights, 1, heights.length);
|
||||||
|
heights=newHeights;
|
||||||
|
for(int i=0;i<heights.length;i++){
|
||||||
|
while(!deque.isEmpty()&&heights[deque.peekLast()]>heights[i]){
|
||||||
|
// 出栈
|
||||||
|
int cur=deque.pollLast();
|
||||||
|
int left=deque.peekLast()+1;
|
||||||
|
res=Math.max(res,(i-left)*heights[cur]);
|
||||||
|
}
|
||||||
|
deque.offerLast(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue