peacekim
할 수 있는 것과 할 수 없는 것.
peacekim
전체 방문자
오늘
어제
  • 분류 전체보기 (68)
    • 👨‍🏫ps (44)
      • ❄️프로그래머스 (20)
      • 🔟0️⃣백준 (21)
      • leetcode (3)
    • ✍🏻study (20)
      • 👐java (6)
      • 🍃spring (1)
      • 🥇algorithm (0)
      • 🚘oodp (4)
      • 📒 jpa (3)
      • 👣DB (2)
      • 🌂네트워크 (0)
      • 🎸기타 (3)
      • 👊 kotlin (1)
      • 🫥 jvm (0)
    • 📽project (4)
      • 🎀ReBoN (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
peacekim

할 수 있는 것과 할 수 없는 것.

[프로그래머스] 후보키 [python]
👨‍🏫ps/❄️프로그래머스

[프로그래머스] 후보키 [python]

2023. 5. 2. 22:28
반응형

문제

조합과 구현을 섞어 놓은 문제였다. 구현을 잘 한다면 풀 수 있는 문제이다.

 

코드


def solution(relation):
    global answer
    answer = 0
    candidates = []
    for i in range(1,len(relation[0]) + 1): ## 조합
        combination([], i, 0, relation, candidates)
    return len(candidates)

def combination(comb, maxLen, nextN, relation, candidates):
    global answer
    if isInCandidates(comb, candidates): 
        return
    if maxLen == len(comb):
        if canUseCandidateKey(comb, relation) == False:
            return
        answer += 1
        candidates.append(tuple(comb))
        return
    for i in range(nextN, len(relation[0])):
        comb.append(i)
        combination(comb, maxLen, i + 1, relation, candidates)
        comb.pop(-1)

def canUseCandidateKey(comb, relation):
    allValues = []
    for r in relation:
        tmp = []
        for i in comb:
            tmp.append(r[i])
        if tmp in allValues:
            return False
        allValues.append(tmp)
    return True
def isInCandidates(comb, candidates):
    for i in candidates:
        cnt = 0
        for k in list(i):
            if k in list(comb):
                cnt += 1
        if cnt == len(i): 
            return True
    return False

"""
완전 탐색
comb에 있는 값들은 어떤 컬럼을 쓰는 지,
"""
반응형

'👨‍🏫ps > ❄️프로그래머스' 카테고리의 다른 글

[프로그래머스] 미로 탈출 [python]  (0) 2023.05.21
[프로그래머스] 혼자 놀기의 달인 [python]  (0) 2023.05.07
[프로그래머스] 문자열 압축 [python]  (0) 2023.04.29
[프로그래머스] 점 찍기 [python]  (0) 2023.04.24
[프로그래머스] 숫자 카드 나누기 [python]  (0) 2023.04.22
    '👨‍🏫ps/❄️프로그래머스' 카테고리의 다른 글
    • [프로그래머스] 미로 탈출 [python]
    • [프로그래머스] 혼자 놀기의 달인 [python]
    • [프로그래머스] 문자열 압축 [python]
    • [프로그래머스] 점 찍기 [python]
    peacekim
    peacekim
    할 수 있는 것과 할 수 없는 것. github: https://github.com/PyeongGangKim

    티스토리툴바