Largest Rectangle in Histogram
2013-05-20 15:56
225 查看
解题思路:以每个柱子作为高, 向左右搜出最大的宽度,会求出n个值, 从而这n个值中的最大值就是结果。
直接贴代码后超时, 受帖子/article/5158301.html启发,剪枝后AC。
直接贴代码后超时, 受帖子/article/5158301.html启发,剪枝后AC。
public class Solution { public int bound; public int largestRectangleArea(int[] height) { // Start typing your C/C++ solution below // DO NOT write int main() function int max = 0; int len = height.length; bound = 0; for (int i = 0; i < len; i++) bound += height[i]; for (int i = len - 1; i >= 0; i--) { if (bound > max) { int wide = 1; for (int j = i - 1; j >= 0; j--) if (height[j] >= height[i]) wide++; else break; for (int j = i + 1; j < len; j++) if (height[j] >= height[i]) wide++; else break; int tem = height[i] * wide; if (tem > max) max = tem; } } return max; } }
相关文章推荐
- HDU 1506 Largest Rectangle in a Histogram——递推
- Largest Rectangle in a Histogram (单调队列)
- HDU 1506 Largest Rectangle in a Histogram(单调队列)
- 84. Largest Rectangle in Histogram
- Leetcode | Largest Rectangle in Histogram
- HDU - 1506 Largest Rectangle in a Histogram(最大子矩阵)
- Leetcode - Largest Rectangle in Histogram
- (java)leetcode-84:Largest Rectangle in Histogram
- Leetcode_84 Largest Rectangle in Histogram
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- 解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- [Leetcode]Largest Rectangle in Histogram
- leetcode-Largest Rectangle in Histogram
- HDU ACM 16 1506 Largest Rectangle in a Histogram
- Largest Rectangle in Histogram