반응형
프로그래머스 : Level 2 피로도 (완전 탐색) 문제
https://school.programmers.co.kr/learn/courses/30/lessons/87946
풀이 : 순열 사용
접근법 : 일단 던전 가는 경우의 수 모두 구한 뒤 무작정 피로도 확인하기
던전 [0, 1, 2] 가 있을 경우
던전이 3개일 경우 가는 경우의 수 (순열 permutations)
[0, 1, 2] [0, 2, 1]
[1, 2, 0] [1, 0, 2]
[2, 0, 1] [2, 1, 0]
이거를 일단 for loop로 구해보자 (재귀 함수 쓰면 될듯) .........포기!
그냥 itertools 라이브러리를 사용하면 엄청 쉽게 순열을 구할 수 있다
직접 재귀함수를 짜서 구할 수도 있는데 어렵다!!
import itertools
def solution(k, dungeons):
answer = 0
# 최소 피로도, 소모 피로도, 최대한 많이 던던 탐험, 현재 피로도 k
# [0,1,2] [0,2,1]
# [1,2,0] [1.0,2]
# [2,0,1] [2,1,0]
for i in itertools.permutations(dungeons, len(dungeons)):
hp = k
survive = 0
dun_per = list(i)
for dungeon in dun_per:
if hp >= dungeon[0]:
hp -= dungeon[1]
survive += 1
if survive > answer:
answer = survive
else:
continue
return answer
참고자료) itertools 없이 순열 구하기
https://yangnyang.tistory.com/14
728x90
반응형
'코딩 테스트 > Programmers' 카테고리의 다른 글
[Python] [Level 2] 덧칠하기 (파이썬 tmp 활용하기) (0) | 2023.03.10 |
---|---|
[Python] [Level 2] 할인 행사 (dictionary로 풀기) (0) | 2023.03.08 |
[Python] [Level 2] 전화번호 목록 (Hash 해시 : 파이썬 dictionary 활용하기) (0) | 2023.03.06 |
[Python] [Level 2] 프린터 (Priority Queue 우선순위 큐) (0) | 2023.03.03 |
[Python] [Level 2] [1차] 뉴스 클러스터링 (feat. 다중 집합의 교집합, 합집합) (isalpha() 함수 이용하기) (0) | 2023.03.02 |