-
[프로그래머스 49993번] 스킬트리Coding Test/프로그래머스 2020. 11. 17. 18:53
# 문제 설명
선행 스킬이란 어떤 스킬을 배우기 전에 먼저 배워야 하는 스킬을 뜻합니다.
예를 들어 선행 스킬 순서가 스파크 → 라이트닝 볼트 → 썬더일때, 썬더를 배우려면 먼저 라이트닝 볼트를 배워야 하고, 라이트닝 볼트를 배우려면 먼저 스파크를 배워야 합니다.
위 순서에 없는 다른 스킬(힐링 등)은 순서에 상관없이 배울 수 있습니다. 따라서 스파크 → 힐링 → 라이트닝 볼트 → 썬더와 같은 스킬트리는 가능하지만, 썬더 → 스파크나 라이트닝 볼트 → 스파크 → 힐링 → 썬더와 같은 스킬트리는 불가능합니다.
선행 스킬 순서 skill과 유저들이 만든 스킬트리1를 담은 배열 skill_trees가 매개변수로 주어질 때, 가능한 스킬트리 개수를 return 하는 solution 함수를 작성해주세요.
# 제한 조건
· 스킬은 알파벳 대문자로 표기하며, 모든 문자열은 알파벳 대문자로만 이루어져 있습니다.
· 스킬 순서와 스킬트리는 문자열로 표기합니다.
- 예를 들어, C → B → D 라면 CBD로 표기합니다
· 선행 스킬 순서 skill의 길이는 1 이상 26 이하이며, 스킬은 중복해 주어지지 않습니다.
· skill_trees는 길이 1 이상 20 이하인 배열입니다.
· skill_trees의 원소는 스킬을 나타내는 문자열입니다.
- skill_trees의 원소는 길이가 2 이상 26 이하인 문자열이며, 스킬이 중복해 주어지지 않습니다.
# 입출력 예
skill skill_trees return
"CBD" ["BACDE", "CBADF", "AECB", "BDA"] 2 # 입출력 예 설명
· BACDE: B 스킬을 배우기 전에 C 스킬을 먼저 배워야 합니다. 불가능한 스킬트립니다.
· CBADF: 가능한 스킬트리입니다.
· AECB: 가능한 스킬트리입니다.
· BDA: B 스킬을 배우기 전에 C 스킬을 먼저 배워야 합니다. 불가능한 스킬트리입니다.
내 풀이
def solution(skill, skill_trees): answer = 0 for tree in skill_trees: skill_count = 0 # 선행 스킬 순서 중 현재 배울 수 있는 스킬의 인덱스 tree_possible = True # 스킬 트리 가능성 여부 for s in tree: if s not in skill: # 해당 스킬이 선행 스킬 순서에 없다면, 바로 배움 continue # 해당 스킬이 선행 스킬 순서에 있다면 if s == skill[skill_count]: # 지금 배울 수 있다면 배움 skill_count += 1 else: # 선행 스킬이 없다면, 배울 수 없는 스킬트리로 간주 tree_possible = False break if tree_possible: answer += 1 return answer
'Coding Test > 프로그래머스' 카테고리의 다른 글
[프로그래머스 42577번] 전화번호 목록 (0) 2020.12.19 [프로그래머스 12899번] 124 나라의 숫자 (0) 2020.11.17 [프로그래머스 42586번] 기능개발 (0) 2020.11.17 [프로그래머스 42576번] 완주하지 못한 선수 (0) 2020.11.17 [프로그래머스 68644번] 두 개 뽑아서 더하기 (0) 2020.11.17