Algorithm/BOJ

[프로그래머스] 완주하지 못한 선수 C# 풀이

Bueong_E 2023. 1. 19. 20:06
반응형
SMALL

제대로 만든줄 알았는데 동명이인 처리도 안했고 심지어  Dictionary<T>를 이용하지도 않았다(ㅎㅎ;;)

아래는 처음 만든 잘못된 틀린 답 그 아래는 Dictionary<T> 를 이용한 풀이

 

1. 잘못된 문제풀이 (이유는 중복된 동명이인 처리가 불가능했고 Dictionary<T>를 이용하지 않음)

using System;
using System.Collections.Generic;
using System.Linq;

namespace BOJ00
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] participant = { "marina", "josipa", "nikola", "vinko", "filipa" };
            string[] completion = { "josipa", "filipa", "marina", "nikola" };

            Console.WriteLine(Solution(participant, completion));
        }

        public static string Solution(string[] participant, string[] completion)
        {
            string answer = "";

            //여기서 부터 작성 

            Dictionary<int, string[]> compareDic = new Dictionary<int, string[]>();   // 천체 선수 배열과 완주 선수 배열을 합친 딕셔너리 생성

            compareDic.Add(100, participant);         // Key 100 value 전체 배열
            compareDic.Add(200, completion);              // Key 200 value 완주 선수 배열

            for (int i = 0; i < participant.Length; i++)     //전체 배열을 돌며 완주선수 배열과비요
            {
                if (compareDic[200].Contains(compareDic[100][i])) continue;
                else
                {
                    answer = compareDic[100][i];           //없는 이름이 있다면 answer에 담아주고 breack;
                    break;
                }

            }
            return answer;                                  //미완주한 선수를 반환
        }
    }
}

결과창

 Dictionary<T>를 이용한 문제풀이

using System;
using System.Collections.Generic;

namespace BOJ00
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] participant0 = { "leo", "kiki", "eden" };
            string[] completion0 = { "eden", "kiki" };
            string[] participant1 = { "marina", "josipa", "nikola", "vinko", "filipa" };
            string[] completion1 = { "josipa", "filipa", "marina", "nikola" };
            string[] participant2 = { "mislav", "stanko", "mislav", "ana" };
            string[] completion2 = { "stanko", "ana", "mislav" };

            Console.WriteLine(Solution(participant0, completion0));
            Console.WriteLine(Solution(participant1, completion1));
            Console.WriteLine(Solution(participant2, completion2));
        }

        public static string Solution(string[] participant, string[] completion)
        {
            string answer = "";

            //여기서 부터 작성 
            // 선수 인원수를 포함한 명단 해시를 생성
            Dictionary<string, int> compareDic = new Dictionary<string, int>();
            //전체 선수 목록에서 이름을 키 값으로 중복이 아니라면 1 중복이면 +1
            foreach (string key in participant)
            {

                if (compareDic.ContainsKey(key))
                    compareDic[key]++;
                else
                    compareDic.Add(key, 1);
            }
            //완주자 배열에서 요소를 가져와 선수명단 해시의 key 값과 비교후 있다면 value -1
            foreach (string name in completion)
            {
                if (compareDic.ContainsKey(name))
                    compareDic[name]--;
            }
            //value 값이 0 인 선수를 answer 에 넣고 반환
            foreach (KeyValuePair<string, int> key in compareDic)
            {
                if (key.Value != 0)
                    answer = key.Key;
            }
            return answer;
        }
    }
}

결과창

Dictionary를 이용하지 않고 할수 있을것만 같았고 할수는 있었지만 코드도 지저분해지고 효율도 많이 떨어졌다.

덕분에 처음부터 다시생각하고 작성하고 Dictionary를 이용하느라 좀 애먹었지만 훨씬 빠르게 풀수 있었다.

좀더 컬렉션에 익숙해질 필요가 있을듯하다.

반응형
LIST