주녘공부일지

[프로그래머스 C#] Lv.1 달리기 경주 본문

Programmers - C#/CodingTest Lv.1

[프로그래머스 C#] Lv.1 달리기 경주

주녘 2023. 8. 20. 16:57
728x90

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

 

프로그래머스

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

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

- 딕셔너리의 키 (플레이어 이름), 값 (플레이어 순위)

    using System;
    using System.Collections.Generic;

    public class Solution
    {
        public string[] solution(string[] players, string[] callings)
        {
            Dictionary<string, int> dict = new Dictionary<string, int>();

            for (int i = 0; i < players.Length; i++)
                dict.Add(players[i], i); // 키 값에 대한 순위

            for (int i = 0; i < callings.Length; i++)
            {
                int num = dict[callings[i]]--; // 현재 순위 + 1
                string str = players[num - 1]; // 추월 당하는 플레이어

                players[num - 1] = players[num];
                players[num] = str;

                dict[str]++;
            }

            return players;
        }
    }
728x90