프로그래머스

의상 (C#)

LemongO 2024. 6. 8. 17:46

📌프로그래머스 12949

💡풀이

  • Dictionary를 사용해 각 의상 종류에 맞는 옷의 개수를 저장
  • 의상을 착용하는 조합을 구하기 위해 각 종류의 의상 수를 곱함(해당 종류의 의상을 안 입는 경우 포함하여 +1)
  • 의상을 안 입는다는 경우는 없으니 answer - 1
public int 의상(string[,] clothes)
{
    int answer = 1;

    Dictionary<string, int> dict = new Dictionary<string, int>();

    for(int i = 0; i < clothes.GetLength(0); i++)
    {
        if (dict.ContainsKey(clothes[i, 1]))
            dict[clothes[i, 1]]++;
        else
            dict.Add(clothes[i, 1], 1);
    }

    foreach(int i in dict.Values)
        answer *= i + 1;

    return --answer;
}