264. 후보키
1) 어떤 전략(알고리즘)으로 해결?
2) 코딩 설명
<내 풀이>
< 내 틀렸던 풀이, 문제점>
from itertools import combinations
def solution(relation):
answer = 0
peoplenum = len(relation)
people = {}
setpeople = {}
for r in range(len(relation)) :
for rr in range(len(relation[r])) :
if rr not in people and rr not in setpeople:
people[rr] = [] ; setpeople[rr] = []
people[rr].append(relation[r][rr])
setpeople[rr].append(relation[r][rr])
else :
people[rr].append(relation[r][rr])
setpeople[rr].append(relation[r][rr])
uniq = [] ; miniq = []
elsee = []
for s in range(len(setpeople)) :
if len(set(setpeople[s]))==peoplenum :
uniq.append(s)
else :
elsee.append(s)
combis = [] ; combisdict = {}
for e in range(2,len(elsee)+1) :
comb = list(combinations(elsee,e))
combis.append(comb)
for c in range(len(combis)) :
for cc in range(len(combis[c])) :
kkey = ""
for ccc in combis[c][cc] :
kkey+=str(ccc)
if (int(kkey)) not in combisdict :
combisdict[int(kkey)] = []
for k in range(peoplenum) :
for c in range(len(combis)) :
for cc in range(len(combis[c])) :
cmp = []; ey = ""
for ccc in combis[c][cc] :
cmp.append(people[ccc][k])
ey+=str(ccc)
if cmp not in combisdict[int(ey)]:
combisdict[int(ey)].append(cmp)
for co in combisdict :
if len(combisdict[co]) == peoplenum :
answer+=1
for cco in combisdict :
if str(co) in str(cco) :
combisdict[cco]=[]
answer+=len(uniq)
return answer
<반성 점>
<배운 점>