[Programmers] 위장

ssu_hyun·2022년 10월 15일
0

Data Structure & Algorithm

목록 보기
66/67
using System;
using System.Collections.Generic;

public class Solution
{
    public int solution(string[,] clothes)  // string[,] : 2차원 이상
    {
        int answer = 1;
        
        // 옷을 담을 옷장 딕셔너리 (옷 종류 : 옷을 담을 리스트)
        Dictionary<string, List<string>> closetDic = new Dictionary<string, List<string>>();
        
        // 옷장에 옷 넣기
        
        // 옷 종류만큼 for문 돌리기
        for (int i=0; i< clothes.GetLength(0); i++) 
        {
            // 옷 종류를 Key로 가지고 있지 않다면 생성
            if (!closetDic.ContainsKey(clothes[i, 1]))
            {
                closetDic[clothes[i, 1]] = new List<string>();
            }
            // 해당되는 Key(옷 종류)에 Value(옷 이름) 넣어주기
            closetDic[clothes[i, 1]].Add(clothes[i, 0]);
        }
            
        // 옷장Dic에 저장된 데이터를 활용해 경우의 수 계산
        /* 각 종류별 (옷의 개수 + 1)을 모두 곱한 뒤 -1
          => 옷의 개수 + 1  : 각 종류별 옷을 선택할 수 있는 경우의 수(아예 선택하지 않는 경우 : +1)
          => -1 : 옷은 1개 이상 걸쳐야함
        */
        foreach (KeyValuePair<string, List<string>> kv in closetDic)
        {
            answer *= (kv.Value.Count + 1);
        }
        return --answer;  
    }
}
  • Length vs GetLength()
int[,] two_dimension = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
Console.WriteLine(two_dimension.Length);  // 6
// GetLength(i) : i차원의 길이를 반환해줌
Console.WriteLine(two_dimension.GetLength(0));  // 3
Console.WriteLine(two_dimension.GetLength(1));  // 2

0개의 댓글