作者Leon (Achilles)
站内Prob_Solve
标题Re: [问题] Google Interview Question (4)
时间Wed Mar 6 02:08:47 2013
※ 引述《RockLee (Now of all times)》之铭言:
: ※ 引述《Leon (Achilles)》之铭言:
: : Sorry, I can't understand your writing.
: : Also, the link seems to be wrong...
: : Can you describe it, step by step.
: : For example, there are only 3 words, (a,b,c)
: : how are you going to find the window, for the document
: : [b b a c b b b b c b b a] ?
: 虽然 pseudo code 可能比较短,
: 但由於 interview Google 时必需写 actual code,
: 所以我想还是直接用实际的 Java code 表达我的想法.
: (我假设 window 长度指的是包含的字数, 与每个字的长度无关)
: 以 Leon 大给的例子:
: document: b b a c b b b b c b b a
: index: 0 1 2 3 4 5 6 7 8 9 10 11
: occurence a: 2, 11
: occurence b: 0, 1, 4, 5, 6, 7, 9, 10
: occurence c: 3, 8
: 执行过程及结果会是:
: windowBegin = 0; windowEnd = 3
: windowBegin = 1; windowEnd = 3
: windowBegin = 2; windowEnd = 4
: windowBegin = 3; windowEnd = 11
: windowBegin = 4; windowEnd = 11
: windowBegin = 5; windowEnd = 11
: windowBegin = 6; windowEnd = 11
: windowBegin = 7; windowEnd = 11
: windowBegin = 8; windowEnd = 11
: bestBegin = 1
: bestEnd = 3
: -----------------------------------------------
我的ㄧ些习惯: (大家互相参考一下)
先确定演算法是对的, 作出 complexity,
再用 Pseudo code 写好
确定结构 ( main program, and possible data structure)
再写 Code.
我不用 Java, 所以我只是很快的扫过你的 code
再你的 Program 中,
你决定了 window start 之後用 occurance index 去找 window ending ?
依照我的理解, starting point has N possibility,
and you have K different words.
Thus, the computational complexity should be O(N* K),
Not the O(N* lg(K)) you claim before?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 128.125.20.198
1F:推 seanwu:找出对应当前start的ending是O(logK),他在移除start换下 03/06 04:41
2F:→ seanwu:一个start时,补上目前start的list中下一个index 03/06 04:42
3F:→ seanwu:是必要而且保证是合法的window,TreeMap中总是有K个index 03/06 04:43
4F:→ seanwu:找ending只是找出max index而已 03/06 04:44
5F:→ Leon:我看不懂你写甚麽 03/07 13:38