LeetCode 84. Maximal Rectanglea (O(n)实现)
来源:互联网 发布:java贪吃蛇教程 编辑:程序博客网 时间:2024/05/28 15:10
参考hackersun007的修行之路的题解,里面的几幅图很直观。
用一个栈维护单调递增的高度的下标,维护元素的压入弹出。
代码:
class Solution {public: int largestRectangleArea(vector<int> &height) { height.push_back(-1); // dummy element stack<int> s; int maxx = 0; for (int i = 0; i < height.size(); ) { if (s.empty()==true || height[s.top()]<=height[i]) { s.push(i ++); // -1 will be pushed at last, and nothing will happen } else { int top_index = s.top(); s.pop(); maxx = max(maxx, height[top_index]*(s.empty()? i: i-s.top()-1)); } } return maxx; }};
0 0
- LeetCode 84. Maximal Rectanglea (O(n)实现)
- LeetCode 53. Maximum Subarray O(n)实现
- LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)
- Leetcode - Array - 1. Two Sum (水题,O[n]和O[n^2]实现)
- LeetCode 31. Longest Valid Parentheses O(n)实现
- LeetCode 41. Trapping Rain Water O(n)实现
- LeetCode 75. Sort Colors (O(n) one pass实现)
- LeetCode 209. Minimum Size Subarray Sum (O(n)实现)
- LeetCode-85-Maximal Rectangle 类似上一题,n遍单调栈
- leetcode 152 O(n)解决方案
- LeetCode 70. Climbing Stairs (O(n)时间, O(1)空间实现)
- 3Sum | LeetCode O(N*N) Solution
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- O(n)实现替换空格
- [LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现
- LeetCode 85 Maximal Rectangle (Python详解及实现)
- [LeetCode] Longest Palindromic Substring - O(n)
- java实现二叉树层次遍历
- java对mongodb的操作
- 14 Annotation Junit 异常
- hdu1498 50 years, 50 colors --- 最小点覆盖
- linux i2c驱动开发
- LeetCode 84. Maximal Rectanglea (O(n)实现)
- BeanUtils使用案例
- UVA 11551 - Experienced Endeavour(矩阵快速幂)
- 单例类的实现方法(转载)
- iOS 常用动画集锦
- 推荐Java程序员阅读的书籍
- 编写俄罗斯方块思路
- 信号量、互斥体和自旋锁(信号量/互斥体和自旋锁的区别)
- 昨记