[问题] Google Interview Question (4)

楼主: RockLee (Now of all times)   2013-03-04 18:51:13
原始网址:
http://www.careercup.com/question?id=248673
题目:
Given a document and a query of K words,
how do you find the smallest window that covers all the words at least once
in that document?
(given you know the inverted lists of all K words, that is, for each word,
you have a list of all its occurrrences).
Could someone propose an algorithm in O(n)?
假设 occurrrences lists 是 sorted, 用 TreeMap 应可在 O(N*logK) 解决,
一开始将每个 occurrrences list 最小的丢入 TreeMap 就可得到第一个 window,
(key = index in the document, value = the occurrrences list it belonged & its
index in that occurrrences list),
之后将 TreeMap 最小的移除并加入同一个 occurrrences list 的下一个即可移动
window,
直到任一个 occurrrences list 耗完.
但要在 O(N) 的时间内解决就想不出来了...
不知道板上有没有人有什么 idea?
作者: isnoneval (虚物之海)   2013-03-05 15:26:00
这样移动 window 的时候并不会保证变小吧
楼主: RockLee (Now of all times)   2013-03-05 18:08:00
是的,最小的 window 不见得是最后的 window我会回移动过程中看到的最小 window
作者: Favonia (00010110110001101010100)   2013-03-06 22:24:00
全塞到一个阵列可以吗? http://hpaste.org/83586啊我不小心假设文件中只有这几个字。sorry
作者: seanwu (海恩)   2013-03-07 01:13:00
话说楼上的第30行是不是应该是 max x cur-1 啊(?)觉得头往后跑但尾不动有点奇怪
作者: Favonia (00010110110001101010100)   2013-03-07 11:11:00
对耶 lol 打错了

Links booklink

Contact Us: admin [ a t ] ucptt.com