Leetcode 84. Largest Rectangle in Histogram
来源:互联网 发布:pic16f877a单片机 编辑:程序博客网 时间:2024/06/08 19:00
/** * Basic idea is using a stack to record the ascending heights in the given array. * Every time meet a height that is smaller than the previous one, pop out the statck, find the maximum area from * heights stored in the statck, then push that smaller height into the array. * Rpeat previous steps. */ public class Solution { public int largestRectangleArea(int[] heights) { int maxArea = 0; Stack<Integer> stack = new Stack<>(); for (int i=0; i<=heights.length; i++) { int curr = (i==heights.length) ? 0 : heights[i]; // found a height that destroys the ascending order while (!stack.isEmpty() && curr < heights[stack.peek()]) { int h = heights[stack.pop()]; int w = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, h*w); } // push i into the stack b/c i >= stack.peek() stack.push(i); } return maxArea; }}
0 0
- [Leetcode] 84. Largest Rectangle in Histogram
- LeetCode --- 84. Largest Rectangle in Histogram
- [LeetCode]*84.Largest Rectangle in Histogram
- [leetcode] 84.Largest Rectangle in Histogram
- [leetcode] 84. Largest Rectangle in Histogram
- Leetcode 84. Largest Rectangle in Histogram
- **LeetCode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- 【leetcode】84. Largest Rectangle in Histogram
- LeetCode 84. Largest Rectangle in Histogram
- LeetCode-84.Largest Rectangle in Histogram
- leetcode.84. Largest Rectangle in Histogram
- [leetcode] 【栈】 84. Largest Rectangle in Histogram
- Leetcode 84. Largest Rectangle in Histogram
- [LeetCode] 84. Largest Rectangle in Histogram
- LeetCode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- jquery中常见的几个问题
- Spring AOP 实现原理
- maven部署 pom文件一直有红叉
- ios下拉图片放大
- 枚举应用(1)-熄灯问题
- Leetcode 84. Largest Rectangle in Histogram
- SpringMvc国际化i18n
- iOS10 不能跳转系统设置解决办法(2)
- 深度学习学习资料
- java构造函数的学习总结
- GIT(3)GIT全局配置(用户名和邮箱)
- 深度链接 Deep Link 开发遇到的坑
- button在点击时出现边框
- crontab 定时执行shell脚本