728x90
https://school.programmers.co.kr/learn/courses/30/lessons/178871
소스코드
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
'코딩공부 > 프로그래머스' 카테고리의 다른 글
※[프로그래머스]Lv.1 공원 산책 C# (0) | 2023.06.09 |
---|---|
※[프로그래머스]Lv.1 크기가 작은 부분문자열 C#(long.parse) (0) | 2023.06.08 |
[프로그래머스]Lv.0 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기 C#(LastIndexOf, Substring) (0) | 2023.06.08 |
[프로그래머스]Lv.0 A 강조하기 C#(Replace) (0) | 2023.06.08 |
[프로그래머스]Lv.0 길이에 따른 연산 C#(list.Aggregate) (0) | 2023.06.08 |