이미지를 클릭하시면 문제 링크로 연결됩니다.
import java.util.HashMap;
import java.util.Iterator;
// 4일차 (해시) - 프로그래머스 의상
public class day04Prog42578 {
public static int solution(String[][] clothes) {
HashMap<String, Integer> map = new HashMap<>();
// 옷이 종류별로 얼마나 있는지 확인
for(String[] clothe : clothes){
String type = clothe[1]; // 옷의 종류를 key 값으로 사용하기 위함
map.put(type, map.getOrDefault(type, 0) + 1); // 키 값으로 옷의 종류를 넣고, type에 해당하는 값을 호출 없으면 0을 반환
}
Iterator<Integer> it = map.values().iterator();
int answer = 1;
while (it.hasNext()) {
answer *= it.next().intValue() + 1; // map 에 저장되어 있는 값을 뽑아서 곱해주고 모든 옷 종류에 대해서 안 입는 경우를 추가해주기 위한 +1
}
return answer - 1; // 무조건 하나는 입어야되기 때문에 아무것도 입지 않는 값을 빼준다.
}
public static void main(String[] args){
String[][] clothes1 = {{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"}, {"green_turban", "headgear"}};
solution(clothes1);
String[][] clothes2 = {{"crow_mask", "face"}, {"blue_sunglasses", "face"}, {"smoky_makeup", "face"}};
solution(clothes2);
}
}