代码拉取完成,页面将自动刷新
# Time: O(n^2)
# Space: O(n)
import collections
import itertools
class Solution(object):
def findSecretWord(self, wordlist, master):
"""
:type wordlist: List[Str]
:type master: Master
:rtype: None
"""
def solve(H, possible):
min_max_group, best_guess = possible, None
for guess in possible:
groups = [[] for _ in xrange(7)]
for j in possible:
if j != guess:
groups[H[guess][j]].append(j)
max_group = max(groups, key=len)
if len(max_group) < len(min_max_group):
min_max_group, best_guess = max_group, guess
return best_guess
H = [[sum(a == b for a, b in itertools.izip(wordlist[i], wordlist[j]))
for j in xrange(len(wordlist))]
for i in xrange(len(wordlist))]
possible = range(len(wordlist))
n = 0
while possible and n < 6:
guess = solve(H, possible)
n = master.guess(wordlist[guess])
possible = [j for j in possible if H[guess][j] == n]
# Time: O(n^2)
# Space: O(n)
class Solution2(object):
def findSecretWord(self, wordlist, master):
"""
:type wordlist: List[Str]
:type master: Master
:rtype: None
"""
def solve(H, possible):
min_max_group, best_guess = possible, None
for guess in possible:
groups = [[] for _ in xrange(7)]
for j in possible:
if j != guess:
groups[H[guess][j]].append(j)
max_group = groups[0]
if len(max_group) < len(min_max_group):
min_max_group, best_guess = max_group, guess
return best_guess
H = [[sum(a == b for a, b in itertools.izip(wordlist[i], wordlist[j]))
for j in xrange(len(wordlist))]
for i in xrange(len(wordlist))]
possible = range(len(wordlist))
n = 0
while possible and n < 6:
guess = solve(H, possible)
n = master.guess(wordlist[guess])
possible = [j for j in possible if H[guess][j] == n]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。