코딩테스트_c#/프로그래머스
프로그래머스 ) 연습문제 - 달리기 경주 Level.1_C#
노년인생
2024. 2. 14. 15:23
728x90
반응형
달리기 경주 문제
내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public string[] solution(string[] players, string[] callings) {
List<string> answer = new List<string>();
Dictionary<string, int> resultDic = new Dictionary<string, int>();
List<string> list = new List<string>();
int num = 0;
foreach(string player in players)
{
resultDic.Add(player, num);
num++;
}
foreach(string call in callings)
{
int n = resultDic[call];
string name = players[n - 1];
players[resultDic[call]-1] = call;
//추월당한 사람 이름
//var _key = resultDic.FirstOrDefault(x => x.Value == resultDic[call] - 1).Key;
players[resultDic[call]] = name;
resultDic[name]++; //추월 당한 사람 순위 ++
resultDic[call]--; //추월 하려는 사람 순위 --
}
return players;
}
}
728x90
반응형