문제 풀기/C#

90. 의상

kagan-draca 2025. 3. 12. 13:51

 

기본 틀 :

using System;

public class Solution {
    public int solution(string[,] clothes) {
        int answer = 0;
        return answer;
    }
}

 

문제를 보고 Dictionary에 key 값으로 string 타입을 value로는 List<string> 을 사용해야 한다는 사실은 파악했다.

 

그 다음으로는 반복문을 사용해 매개변수 clothes를 순회하면서 clothes[index, 1]은 key 값으로 clothes[index, 0]는 value 값으로 Dictionary에 담아줬다.

 

여기까지는 무난무난하게 코드를 작성할 수 있었지만

 

얼굴, 상의, 하의, 겉옷으로 중복 없이 경우의 수를 구하는 코드 작성이 어려웠다.

 

이유로는 각 부분의 옷을 입지 않을 수도 있기 때문이었다...

 

위의 문제를 해결하기 위해 인터넷을 찾아보던 중 여사건을 활용하면 된다는 사실을 알게 됐다.

 

ex) 

 

headgear는 "yellow_hat", "green_turban"

 

eyewear는 "blue_sunglasses"

 

가 존재하는데 여기에서 선택하지 않는다 라는 항목을 만들어

 

headgear는 "yellow_hat", "green_turban", "선택 안함"

 

eyewear는 "blue_sunglasses", "선택 안함"

 

각각의 선택 가능한 개수를 모두 곱해준다.

 

3 X 2 = 6

 

이후, headgear와 eyewear를 둘 다 선택하지 않는

 

겨우를 1 빼주어 정답을 구할 수 있었다.

 

 

using System;
using System.Collections.Generic;

public class Solution {
    public int solution(string[,] clothes) 
    {
        Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
        
        for(int i = 0; i < clothes.GetLength(0); i++)
        {
            if(!dictionary.ContainsKey(clothes[i, 1]))
                dictionary[clothes[i, 1]] = new List<string>();
            dictionary[clothes[i,1]].Add(clothes[i,0]);
        }
        
        int count = 1;
        
        foreach(List<string> values in dictionary.Values)
        {
            count *= values.Count + 1;
        }
        
        return count - 1;
    }
}

 

 

'문제 풀기 > C#' 카테고리의 다른 글

92. 프로세스  (0) 2025.03.14
91. 기능 개발  (0) 2025.03.12
89. 할인 행사  (0) 2025.03.11
88. 행렬의 곱셈  (0) 2025.03.11
87. n^2 배열 자르기  (1) 2025.03.07