Algorithm/Python

[Python] 프로그래머스 - 후보키

힘팽 2022. 3. 19. 21:51

◈ 오류 정정 및 피드백 환영

 

코딩테스트 연습 - 후보키

[["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] 2

programmers.co.kr


🔎문제 분석

희희

🤦‍♀️유의 사항

희희


from collections import deque

def solution(relation):
    n = len(relation[0])
    res = []
    def DFS(v, s):
        if v==m:
            res.append(a[:])
        else:
            for i in range(s, n+1):
                a[v]=i-1
                DFS(v+1, i+1)

    # 조합 만들기
    for m in range(1,n+1):
        a = [0]*m
        DFS(0, 1)
    
    # 유일성 확인
    uni = []
    for now in res:
        cand = list(map(lambda x: ''.join(x), zip(*[i for idx, i in enumerate(list(zip(*relation))) if idx in now])))
        if sorted(list(set(cand))) == sorted(list(cand)):
            uni.append(now)
            
   # 최소성 확인
    que = deque(uni)
    answer = 0
    while que:
        now = que.popleft()
        answer += 1
        que = deque(filter(lambda i: (set(now) & set(i))!=set(now), list(que)))
    return answer