今天是代码随想录训练营的第六十天,是最后一天,也代表这一刷结束
class Solution { public int largestRectangleArea(int[] heights) { int n = heights.length; Stack<Integer> stack = new Stack<>(); int max = -1; for(int i = 0; i< n; i++){ while(!stack.isEmpty() && heights[stack.peek()]>=heights[i]){ int temp = stack.peek(); stack.pop(); max = Math.max(max, (stack.isEmpty()?i:(i-1)-stack.peek())*heights[temp]); } stack.push(i); } while(!stack.isEmpty()){ int temp = stack.peek(); stack.pop(); max = Math.max(max,(stack.isEmpty()?n:(n-1-stack.peek()))*heights[temp]); } return max; } }
要理解空栈和自然弹出所代表的含义。
代码随想录训练营刷完了,刷题带给我很大的自信,在刷之前还是对未来很迷茫的,刷的过程中开始投简历,找到了一个目前蛮满意的工作,期待二刷!加油!
标签:peek,int,max,训练营,随想录,heights,stack,单调 From: https://www.cnblogs.com/catSoda/p/16972805.html