ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

二刷hot100-84.柱状图中最大的矩形

2026/8/20 15:47:01 拓冰建站 浏览量
二刷hot100-84.柱状图中最大的矩形 还是单调栈与之前不同的是之前要求栈内元素递减这道题要求递增且求取矩形面积需要用到三个元素当前遍历元素、栈口弹出元素当前栈口元素所以栈内始终需要有一个元素定义一个新数组相比之前的数组在起始和结尾各加一个新元素0class Solution { public int largestRectangleArea(int[] heights) { int[] h new int[heights.length 2]; StackInteger s new Stack(); System.arraycopy(heights,0,h,1,heights.length); int res 0; for(int i 0;i h.length;i){ if(s.isEmpty()){ s.push(i); }else if(h[i] h[s.peek()]){ s.push(i); }else{ while(!s.isEmpty() h[i] h[s.peek()]){ int k s.pop(); res Math.max(res,(i - s.peek() - 1) * h[k]); } s.push(i); } } return res; } }