Python/알고리즘

완전탐색 Level 2 피로도 - python (프로그래머스)

구름용 2023. 6. 23. 17:02

소스 코드

from itertools import permutations
def solution(k, dungeons):
    
    path_list = list(permutations(dungeons, len(dungeons)))
    answer_list = []
    for path in path_list:
        cur_fatigue = k
        count = 0
        for dungeon in path:
            if cur_fatigue < dungeon[0]:
                break
            cur_fatigue -= dungeon[1]
            count += 1
        answer_list.append(count)
        
    
    return max(answer_list)

풀이

문제의 제한사항에서 던전의 개수는 1이상 8이하라고 하였으므로 이 또한 모든 던전의 형태를 구한 다음 각 던전을 순회하면서 solution을 구하는 방식을 택하였다.

 

먼저 permutations를 사용하여 모든 던전을 탐험하는 path를 구한 다음 해당 path를 순회하며 조건에 맞을 경우 count를 1씩 증가하였다.