프로그래머스/lv.1

(프로그래머스) lv.1 달리기 경주

bingual 2024. 1. 16. 19:15
반응형

 

https://school.programmers.co.kr/learn/courses/30/lessons/178871

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제풀이

배열을 이용해서 풀이하게 된다면 시간초과가 나기 때문에 딕셔너리로 풀이

def solution(players, callings):
    p_dict = {p: i for i, p in enumerate(players)}  # 플레이어 : 순위
    r_dict = {i: p for i, p in enumerate(players)}  # 순위 : 플레이어

    for call in callings:
        temp = r_dict[p_dict[call] - 1]  # 추월당한 선수 이름

        # 추월한 선수와 추월 당한 선수의 이름을 바꿈
        r_dict[p_dict[call] - 1] = r_dict[p_dict[call]]
        r_dict[p_dict[call]] = temp

        # 추월한 선수와 추월 당한 선수의 순위 갱신
        p_dict[call] -= 1
        p_dict[temp] += 1

    answer = list(r_dict.values())
    return answer