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

출처: 프로그래머스 코딩 테스트 연습
https://school.programmers.co.kr/learn/courses/30/lessons/178871

 

 

나의 풀이

public string[] solution(string[] players, string[] callings) 
{
    var dict = players.Select((s, i) => new{name = s, index = i})
                      .ToDictionary(d => d.name, d => d.index);

    foreach(string call in callings)
    {
        int rank = dict[call]; // 추월하려는 사람의 순위
        string overtaken = players[rank - 1]; // 추월당한 사람의 이름

        players[rank - 1] = call;
        players[rank] = overtaken;

        dict[call] = rank - 1;
        dict[overtaken] = rank;
    }

    return players;
}

 

신규 추가된 문제.

추월시에는 순위가 한개만 바뀌기 때문에 딕셔너리는 한 개만 써도 된다.

Lv1이라서 아마 실행시간도 널널했을 듯하다. 간단한 문제.

 

 

댓글

Designed by JB FACTORY