SMALL
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr

문제 풀이
문제 조건에 최소 1파츠를 입는다고 했다.
그렇다면 모든 경우의 수 - 아무것도 안입는 경우 (1) 을 해주면 된다.
코드
import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Iterator;
class Solution {
public int solution(String[][] clothes) {
Map<String, Integer> clothesHash = new HashMap<String, Integer>();
for(int i = 0; i < clothes.length; i++) {
if(clothesHash.containsKey(clothes[i][1])) {
clothesHash.put(clothes[i][1], clothesHash.get(clothes[i][1]) + 1);
} else {
clothesHash.put(clothes[i][1], 2);
}
}
int answer = 1;
Iterator <Entry<String, Integer>> entry = clothesHash.entrySet().iterator();
while(entry.hasNext()) {
Map.Entry<String, Integer> e = entry.next();
answer *= e.getValue();
}
return (answer - 1);
}
}
처음 해시를 만들 때 1이아닌 2로 하는 이유는 경우의 수를 계산할때 해당 파츠를 안입는경우를 함께 포함시켜 넣는 것이다. (파츠가없다면 애초에 계산할필요는 없음)
파츠별로 해시에 들어있는 value를 곱한 후 마지막에 아무것도 입지 않는경우를 빼 준다.
예시를 직접 손으로 그려봤다면 빠르게 해결할 수 있는 문제인 것 같다.
+) 문제 풀고 나서 다른 사람들의 답을 구경하러갔는데.. 정말 엄청난 풀이가 있었다... 잘 안써본 내용이라 공부하고 잘 정리해서 붙여야겠다
SMALL