由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 谁给解释解释这题?
相关主题
请教一个常见的面试题的答案真慫阿, Facebook 1st phone interview,
问个算法题3这题咋做?
问一道老题问个Facebook 电面题
一道老题找最近的点,这题咋解?
lint code 这个counter numbers smaller than met店面经
2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么leetcode新题怎么做?
你如何给一个百万页的书建立index?面试题:两个有序数组中的最小差值
问两道微软题请教一道google的数组遍历题
相关话题的讨论汇总
话题: index话题: bar话题: right话题: left话题: popped
进入JobHunting版参与讨论
1 (共1页)
r*******k
发帖数: 1423
1
https://oj.leetcode.com/problems/largest-rectangle-in-histogram/
我写了个递归的,类似二分查找的,
在测试case {0,1,2,...8893}时,直接stackOverFlow了
看了神解答,
https://oj.leetcode.com/discuss/oj/largest-rectangle-in-histogram
但是没看懂
主要是:为啥当前的index就是right_index?
h*******e
发帖数: 1377
2
类似单调队列dp, 但是这里用stack 维护一个单调的栈 数据结构不是deque
r*******k
发帖数: 1423
3
算法解释是这样的:
We have discussed a Divide and Conquer based O(nLogn) solution for this
problem. In this post, O(n) time solution is discussed. Like the previous
post, width of all bars is assumed to be 1 for simplicity. For every bar ‘x
’, we calculate the area with ‘x’ as the smallest bar in the rectangle.
If we calculate such area for every bar ‘x’ and find the maximum of all
areas, our task is done. How to calculate area with ‘x’ as smallest bar?
We need to know index of the first smaller (smaller than ‘x’) bar on left
of ‘x’ and index of first smaller bar on right of ‘x’. Let us call these
indexes as ‘left index’ and ‘right index’ respectively.
We traverse all bars from left to right, maintain a stack of bars. Every bar
is pushed to stack once. A bar is popped from stack when a bar of smaller
height is seen. When a bar is popped, we calculate the area with the popped
bar as smallest bar. How do we get left and right indexes of the popped bar
– the current index tells us the ‘right index’ and index of previous item
in stack is the ‘left index’. Following is the complete algorithm.

【在 h*******e 的大作中提到】
: 类似单调队列dp, 但是这里用stack 维护一个单调的栈 数据结构不是deque
r*******k
发帖数: 1423
4
里面明明说要两边找,left index和right index
可是,只扫描一遍,怎么找right index?
算的时候,为啥right index就是自己?

‘x
left
these

【在 r*******k 的大作中提到】
: 算法解释是这样的:
: We have discussed a Divide and Conquer based O(nLogn) solution for this
: problem. In this post, O(n) time solution is discussed. Like the previous
: post, width of all bars is assumed to be 1 for simplicity. For every bar ‘x
: ’, we calculate the area with ‘x’ as the smallest bar in the rectangle.
: If we calculate such area for every bar ‘x’ and find the maximum of all
: areas, our task is done. How to calculate area with ‘x’ as smallest bar?
: We need to know index of the first smaller (smaller than ‘x’) bar on left
: of ‘x’ and index of first smaller bar on right of ‘x’. Let us call these
: indexes as ‘left index’ and ‘right index’ respectively.

r*******k
发帖数: 1423
5
被这题搞伤心了,sigh

【在 r*******k 的大作中提到】
: 里面明明说要两边找,left index和right index
: 可是,只扫描一遍,怎么找right index?
: 算的时候,为啥right index就是自己?
:
: ‘x
: left
: these

h*******e
发帖数: 1377
6
算法是求当前坐标为正方形(还是长方形)右沿儿的所有最大面积可能。
左沿的情况那个stack结构在 index 遍历经过左沿的时候已经记录了。。
我当时也想了好久,后来出去吃饭时候想明白了,但是最后发现结束的时候还有一个特
殊条件没考虑。

‘x
left
these

【在 r*******k 的大作中提到】
: 算法解释是这样的:
: We have discussed a Divide and Conquer based O(nLogn) solution for this
: problem. In this post, O(n) time solution is discussed. Like the previous
: post, width of all bars is assumed to be 1 for simplicity. For every bar ‘x
: ’, we calculate the area with ‘x’ as the smallest bar in the rectangle.
: If we calculate such area for every bar ‘x’ and find the maximum of all
: areas, our task is done. How to calculate area with ‘x’ as smallest bar?
: We need to know index of the first smaller (smaller than ‘x’) bar on left
: of ‘x’ and index of first smaller bar on right of ‘x’. Let us call these
: indexes as ‘left index’ and ‘right index’ respectively.

a****g
发帖数: 16
7
我这里有个解释,仅供参考:
http://decomplexify.blogspot.com/2014/03/algorithm-max-rectangl

【在 r*******k 的大作中提到】
: 被这题搞伤心了,sigh
r*******k
发帖数: 1423
8
你这个blog真好
我要rss订阅,hoho

【在 a****g 的大作中提到】
: 我这里有个解释,仅供参考:
: http://decomplexify.blogspot.com/2014/03/algorithm-max-rectangl

a****g
发帖数: 16
9
欢迎光临!

【在 r*******k 的大作中提到】
: 你这个blog真好
: 我要rss订阅,hoho

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一道google的数组遍历题lint code 这个counter numbers smaller than me
minMSwap 这题能比O(n^2)更快的解法吗2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
问个Array Puzzle题你如何给一个百万页的书建立index?
O(NlogN) largest rectangle in histogram问两道微软题
请教一个常见的面试题的答案真慫阿, Facebook 1st phone interview,
问个算法题3这题咋做?
问一道老题问个Facebook 电面题
一道老题找最近的点,这题咋解?
相关话题的讨论汇总
话题: index话题: bar话题: right话题: left话题: popped