알고리즘 풀이/프로그래머스
[level2] 프로그래머스 - 위장(JAVA)
데롱디롱
2021. 9. 15. 01:01
728x90
- 물건을 선택하는 경우의 수
물건이 N, M, K. J개 있는 경우
(N + 1) * (M + 1) * (K + 1) * (J + 1) - 1
- '+1'해주는 이유는, 해당 물건을 고르지 않는 경우
- 마지막에 '-1'해주는 이유는, 모든 물건을 고르지 않는 경우를 빼 주어야 되기 때문
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
HashMap<String, Integer> dress = new HashMap<>();
for(String[] c : clothes)
dress.put(c[1], dress.containsKey(c[1]) ? dress.get(c[1]) + 1 : 1);
int answer = 1;
for(Integer num : dress.values())
answer *= num + 1;
return answer - 1;
}
}