google的题果然都不是吃素的,值得细品啊,都是leetcode上的hard题变形+组合,如此题:
Circuit Board,先从相对简单的set 1开始解, 按照官方解析中的“we first define Pi,j as the maximum number of contigious cells on the right of the cell i, j . We immediately observe that Pi, j = 1 + Pi, j + 1 if the integers in j and (j+1)th columns of row i are equal, else Pi,j is 1.”,我们得到了一个新的矩阵,注意到此矩阵的转置矩阵可用单调栈解Largest Rectangle in Histogram的方法解。
解了Set1, Set2其实也不难了,问题在于怎样高效构造矩阵。我的第一反应是构造Sparse Table。这样时间是O(RClog(C ))。但是官方提示有更快的解法。我思考良久。。终于发明了自己的单调队列解法。它是线性的,所以总体时间是O(RC)+O(RC)=O(RC)
竖向使用单调栈+横向使用单调队列,完美解决kick start c轮 Circuit Board
