알고리즘 풀이/프로그래머스

[프로그래머스][JAVA] 위장 (해시, 집합)

배게 2022. 3. 27. 08:23
728x90

집합 A,B,... ,N들의 조합의 수

(A의 개수+1) * (B의 개수+1) * ... * (N의 개수 +1) - 1(공집합)

집합의 개수에서 1을 더하는 이유는 집합의 원소에서 null을 하나 더하는 것과 같다

 

마지막에 공집합으로 -1을 빼는 이유는 A,B,...,N의 집합들에 임의로 넣은 null의 원소들로만

이루어진 경우의 수를 빼는거임

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.*;
 
class Solution {
    public int solution(String[][] clothes) {
        Map<String, Integer> map = new HashMap<>();
        for(int i=0; i<clothes.length; i++){
            // System.out.println(clothes[i][1]);
            String type = clothes[i][1];
            map.put(type, map.getOrDefault(type,0)+1);
        }
        int answer = 1;
        for(Map.Entry<String,Integer> e : map.entrySet()){
            answer*=(e.getValue()+1);
        }
        
        
        return --answer;
    }
}
cs