코딩테스트/프로그래머스

[프로그래머스] Level 2 - 양궁대회 (Python)

ye0nn 2022. 5. 24. 23:09

 

 

 

 

https://programmers.co.kr/learn/courses/30/lessons/92342

 

코딩테스트 연습 - 양궁대회

문제 설명 카카오배 양궁대회가 열렸습니다. 라이언은 저번 카카오배 양궁대회 우승자이고 이번 대회에도 결승전까지 올라왔습니다. 결승전 상대는 어피치입니다. 카카오배 양궁대회 운영위원

programmers.co.kr

 

 

 

 

✅ 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