Java
프로그래머스: 의상
Daeryuk Kim
2024. 3. 7. 14:19
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
HashMap<String, Integer> clothesMap = new HashMap<>();
for (String[] cloth : clothes) {
String category = cloth[1]; // 각 행의 옷의 종류를 뽑아옴.
clothesMap.put(category, clothesMap.getOrDefault(category, 0) + 1);
}
int answer = 1;
for (int value : clothesMap.values()) {
/*
이 부분은 각 옷의 종류별로 착용할 수 있는 경우의 수를 계산하는 부분입니다.
예를 들어, "headgear"(모자) 종류의 옷이 두 개 있다면, 이를 착용하는 경우의 수는 다음 세 가지입니다:
1: "yellow_hat" 착용
2: "green_turban" 착용
3: 모자를 착용하지 않음
안 입는 경우를 고려해서 +1를 해주고 answer에 곱해주는 거지~
*/
answer *= (value + 1);
}
return answer - 1; // 아무것도 입지 않는 경우를 빼줌
}
public static void main(String[] args) {
String[][] clothes = {{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"}, {"green_turban", "headgear"}};
Solution sol = new Solution();
System.out.println(sol.solution(clothes));
}
}