https://programmers.co.kr/learn/courses/30/lessons/92342
✅ Solution
- combinations_with_replacements를 이용하여 라이언이 쏠 수 있는 점수들의 조합, candidates를 모두 구한다.
- candidates를 돌면서 라이언의 점수를 계산해주고, 어피치 점수와 비교한다.
- 점수 차이가 같을 때는 낮은 점수의 비중이 큰 경우를 리턴하라고 했으니 낮은 점수를 먼저 채우면서 라이언의 점수를 만든다.
- 그렇게 되면, 낮은 점수의 조합들이 먼저 answer에 들어가기 때문에 위 조건을 만족하는 답을 구할 수 있다.
- 라이언 점수가 어피치 점수보다 클 때, 그 차이를 비교하며 maxGap, answer을 갱신해준다.
✅ Code
from itertools import combinations_with_replacement
def solution(n, info):
answer = [-1]
maxGap = -1e9
candidates = list(combinations_with_replacement(range(0, 11), n))
for candidate in candidates:
info2 = [0] * 11
apeach, lion = 0, 0
for score in candidate:
info2[10 - score] += 1
for score, (a, l) in enumerate(zip(info, info2)):
if a == l == 0:
continue
elif a >= l:
apeach += (10 - score)
else:
lion += (10 - score)
if lion > apeach:
gap = lion - apeach
if gap > maxGap:
maxGap = gap
answer = info2
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 더 맵게 (Python) (0) | 2022.06.03 |
---|---|
[프로그래머스] Level 2 - 주차 요금 계산 (Python) (0) | 2022.05.24 |
[프로그래머스] Level 2 - [3차] 압축 (Python) (0) | 2022.05.20 |
[프로그래머스] Level 2 - [3차] 방금그곡 (Python) (0) | 2022.05.18 |
[프로그래머스] Level 2 - [1차] 캐시 (Python) (0) | 2022.05.17 |