DHistory

[Programmers] Level 1 - 달리기 경주 본문

Computer Science/Algorithm

[Programmers] Level 1 - 달리기 경주

ddu0422 2023. 6. 11. 21:27

문제

 

프로그래머스

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

programmers.co.kr

 

풀이

def solution(players, callings):    
     # 초기화
    players_map = {}

    for (index, value) in enumerate(players):
        players_map.update({value: index})  

    # 문제풀이
    for calling in callings:
        # 현재 순위 가져오기
        index = players_map[calling]
        front_name = players[index - 1]

        # 랭크(이름) 변경
        players[index], players[index - 1] = players[index - 1], players[index]

        # 랭크(순위) 변경
        players_map[calling] = index - 1
        players_map[front_name] = index

    players_map = sorted(players_map.items(), key=lambda x: x[1])

    # 정답 구하기
    answer = [key for (key, _) in players_map]

    return answer

 

채점 결과