사용 언어 - Python3
문제 - 피로도
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답
순열을 활용한 완전 탐색 문제 (정답 맞춘 여부 X)
정답 풀이
from itertools import permutations
permutations(list명, 원소개수) = 원소개수만큼 순열 만들기!!!
현재 피로도 k
dungeons = [최소 필요 피로도,탐험후 소모 피로도]
방법1. answer = [] 빈 리스트를 만들어서 cnt 값 저장해주고, max값을 출력.
방법2. answer = -1 cnt값과 answer 값 비교해서 max값 출력. answer = max(answer,cnt)
# 방법1
from itertools import permutations
def solution(k, dungeons):
answer = []
for d in permutations(dungeons,len(dungeons)):
limit = k
cnt = 0
for min_cost, cost in d:
if limit >= min_cost:
limit -= cost #남은 피로도
cnt += 1
answer.append(cnt)
return max(answer)
#방법2
from itertools import permutations
def solution(k, dungeons):
answer = -1
for d in permutations(dungeons,len(dungeons)):
limit = k
cnt = 0
for min_cost, cost in d:
if limit >= min_cost:
limit -= cost
cnt += 1
answer = max(answer,cnt)
return answer
레퍼런스
- 정답 참고
[프로그래머스 87946 파이썬] 피로도 (level 2, 완전 탐색)
순열과 완전 탐색
velog.io
- 깃허브 정답
GitHub - yyeongeun/codingtest: 코딩테스트 공부
코딩테스트 공부. Contribute to yyeongeun/codingtest development by creating an account on GitHub.
github.com
'Algorithm > 완전탐색' 카테고리의 다른 글
[프로그래머스 lv1] 대충 만든 자판 (0) | 2023.04.24 |
---|---|
[프로그래머스 lv 2] 모음사전 (0) | 2023.02.15 |
[프로그래머스 lv 2] 카펫 (0) | 2023.02.07 |
[프로그래머스 lv 2] 소수 찾기 (0) | 2023.02.07 |
[프로그래머스 lv 1] 모의고사 (0) | 2023.02.06 |
댓글