由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode: largest rectangle in histogram求帮助
相关主题
Largest Rectangle in Histogram问个题
Largest Rectangle in Histogram不同算法的复杂度贡献Google电面面经(不小心误删除了,再发一遍并update)
Google interview questionO(NlogN) largest rectangle in histogram
challenge: 找bugleetcode一道题
histogram问题刚看了leetcode 上的maximum rectangular sub matrix那题
问个largest rectangle in histogram的问题求Leetcode OJ Maximal Rectangle的n^2解法
DP算法占用的空间Modified Maximal Rectangle problem from Leetcode
面试遇到像largest rectangle in histogram这样的高难题要怎么办?leetcode container with most water
相关话题的讨论汇总
话题: min话题: index话题: begin话题: height话题: end
进入JobHunting版参与讨论
1 (共1页)
c********t
发帖数: 5706
1
想了一个笨的O(n*logn)方法
1.找到从begin到end最小值min, 得到index_min
2.则从begin index到 end index的rectangle area = min*(end-begin+1)。
3.用recursion重复1,2,计算[begin,index_min-1] 的rectangle面积和[index_min+1
,end]的面积。
过了small test, large test 在[0,1...19999]的例子,得到runtime error。
拷到eclipse测试,通过了。谁能帮我看看有什么问题。
public class Solution {
int max;
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
max = 0;
if(height == null || height.length == 0) return 0;
largest(height, 0, height.length-1);
return max;
}

public void largest(int[] height, int begin, int end){
if(begin==end) {
max= Math.max(max,height[begin]);
return;
}
int min = Integer.MAX_VALUE, index_min=0;
for(int i= begin; i<=end; i++){
if(height[i] < min){
min = height[i];
index_min=i;
}
}

max= Math.max(max, min*(end-begin+1));
if(index_min == begin) largest(height, index_min+1, end);
else if(index_min == end) largest(height, begin, index_min-1);
else{
largest(height, begin, index_min-1);
largest(height, index_min+1, end);
}
}
}
l*******b
发帖数: 2586
2
递归太深,stack 爆了?

+1

【在 c********t 的大作中提到】
: 想了一个笨的O(n*logn)方法
: 1.找到从begin到end最小值min, 得到index_min
: 2.则从begin index到 end index的rectangle area = min*(end-begin+1)。
: 3.用recursion重复1,2,计算[begin,index_min-1] 的rectangle面积和[index_min+1
: ,end]的面积。
: 过了small test, large test 在[0,1...19999]的例子,得到runtime error。
: 拷到eclipse测试,通过了。谁能帮我看看有什么问题。
: public class Solution {
: int max;
: public int largestRectangleArea(int[] height) {

c********t
发帖数: 5706
3
看来是。还是学习用stack的方法吧。 多谢!

【在 l*******b 的大作中提到】
: 递归太深,stack 爆了?
:
: +1

1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode container with most waterhistogram问题
再问Maximal Rectangle的N^2解法问个largest rectangle in histogram的问题
Question about Leetcode: Maximum rectangleDP算法占用的空间
leetcode 3sum runtime 一問面试遇到像largest rectangle in histogram这样的高难题要怎么办?
Largest Rectangle in Histogram问个题
Largest Rectangle in Histogram不同算法的复杂度贡献Google电面面经(不小心误删除了,再发一遍并update)
Google interview questionO(NlogN) largest rectangle in histogram
challenge: 找bugleetcode一道题
相关话题的讨论汇总
话题: min话题: index话题: begin话题: height话题: end