알고리즘/카카오기출
[Python] programmers 양궁대회
정찡이
2022. 1. 23. 00:16
728x90
1. 문제 링크
https://programmers.co.kr/learn/courses/30/lessons/92342
2. 문제 요약
- k점을 많은 화살을 맞힌 선수가 k점을 가져간다.
- 라이언이 가장 큰 점수 차이로 우승하기 위해 n발의 화살을 어떤 과녁 점수에 맞혀야 하는지 10~0점까지 점수를 배열에 담기
3. 아이디어 정리
- 중복조합을 이용해 라이언 점수를 만든다.
- 라이언 점수와 어피치 점수를 비교해서 총점수를 구한다.
- 총점수를 비교해 라이언이 큰 점수를 얻는 경우 업데이트한다.
4. 문제 풀이
4-1. 내 풀이
from itertools import combinations_with_replacement
def solution(n, info):
answer = [0 for _ in range(11)]
win = False
max_num = 0 # 라이언이 이길때 가장큰 점수 차이
# 1. 중복 조합을 이용해 라이언의 점수를 만든다.
for res in list(combinations_with_replacement(range(0, 11), n)):
now = [0 for _ in range(11)]
for r in res:
now[10 - r] += 1
lion = 0
peach = 0
# 2. 라이언 점수와 어피치 점수 비교한다.
for i, (l, p) in enumerate(zip(now, info)):
if l == p == 0:
continue
if p >= l:
peach += (10 - i)
elif l > p:
lion += (10 - i)
# 3. 총 점수를 비교해 라이언이 큰 경우 결과를 업데이트 해준다.
if lion > peach:
win = True
if (lion - peach) > max_num:
max_num = lion - peach
answer = now
if not win:
return [-1]
return answer
5. 결론
- 구현