코딩공부/프로그래머스

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

usingsystem 2023. 6. 8. 22:40
728x90

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

 

프로그래머스

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

programmers.co.kr

소스코드

using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
    public string[] solution(string[] players, string[] callings)
    {
        Dictionary<string, int> dic = players.Select((s, idx) => new { name = s, index = idx }).ToDictionary(d => d.name, d => d.index);

        for (int i = 0; i < callings.Length; i++)
        {
            string calling = callings[i];
            int rank = dic[calling];

            string temp = players[rank - 1];
            players[rank - 1] = calling;
            players[rank] = temp;

            dic[calling] = rank - 1;
            dic[temp] = rank;
        }

        return players;
    }
}

Dictionary를 사용하지않고 array를 사용하여 인덱스로 풀면 시간초과 오류가 남.

728x90