286. 후보키

아현·2021년 9월 1일
0

Algorithm

목록 보기
299/400



1. Python


집합, combinations 사용


from itertools import combinations

def solution(relation):
    # key의 개수 
    col_len = len(relation[0])
    key_idx = list(range(col_len))
    candidate_keys = []
    
    for i in range(1, col_len + 1):
        for comb in combinations(key_idx, i):
            hist = []
            for rel in relation:
                current_key = [rel[c] for c in comb]
                # 하나라도 중복되는 경우: 식별 불가능 
                if current_key in hist:
                    break
                else:
                    hist.append(current_key)
            # 하나도 중복 안 된 경우: 식별 가능
            else:
                for ck in candidate_keys:
                    # 최소성 확인 
                    if set(ck).issubset(set(comb)):
                        break
                else:
                    candidate_keys.append(comb)
    
    return len(candidate_keys)
    
print(solution([["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]]))



profile
Studying Computer Science

0개의 댓글