반응형
1. 문제
정수 배열 score가 주어지고 모든 값은 유니크하다.
가장 높은 점수는 "Gold medal", 두 번째는 "Silver Medal", 세 번째는 "Bronze Meda", 그 외에는 등수로 치환하여 새로운 string 배열을 출력하면 된다.
2. 풀이 과정
score 배열을 sorted라는 배열에 내림차순으로 정렬하며 복사한다.
sorted 배열을 순회하면서 Dictionary<int, string>에 값과 등수로 짝을 맺어 저장한다.
score 배열을 순회하면서 Dictionary에서 매칭되는 등수를 꺼내와 결괏값으로 출력한다.
public class Solution {
public string[] FindRelativeRanks(int[] score) {
var sorted = score.OrderByDescending(x => x).ToArray();
var dict = new Dictionary<int, string>();
var result = new string[score.Length];
for(int i = 1, length = sorted.Length; i <= length; ++i)
{
if(i == 1)
{
dict.Add(sorted[i - 1], "Gold Medal");
}
else if(i == 2)
{
dict.Add(sorted[i - 1], "Silver Medal");
}
else if(i == 3)
{
dict.Add(sorted[i - 1], "Bronze Medal");
}
else
{
dict.Add(sorted[i - 1], i.ToString());
}
}
for(int i = 0, length = score.Length; i < length; ++i)
{
result[i] = dict[score[i]];
}
GC.Collect();
return result;
}
}
반응형
'LeetCode 풀이노트' 카테고리의 다른 글
[C#] 705. Design HashSet (0) | 2023.05.30 |
---|---|
[C#] 598. Range Addition II (0) | 2023.05.26 |
[C#] 944. Delete Columns to Make Sorted (0) | 2023.05.25 |
[C#] 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers (0) | 2023.05.25 |
[C#] 2006. Count Number of Pairs With Absolute Difference K (0) | 2023.05.24 |