파이썬 알고리즘 264번 | [프로그래머스 후보키]

Yunny.Log ·2022년 9월 5일
0

Algorithm

목록 보기
269/318
post-thumbnail

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])

    #print(people)

    # 유일키     # 최소키
    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)
    
    #print(combis)

    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)] = []
                
    #print(combisdict)
    
    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)
    #print(combisdict)
    
    for co in combisdict :
        if len(combisdict[co]) == peoplenum :
            answer+=1 # miniq
            for cco in combisdict :
                if str(co) in str(cco) :
                    combisdict[cco]=[]

    answer+=len(uniq) 
    return answer

  • 마지막에 최소키 거르는 부분

<반성 점>

<배운 점>

0개의 댓글