본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 의상

by 현장 2026. 1. 14.

-Code

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        // map으로 옷들을 분류 및 저장
        Map<String, ArrayList<String>> clothesMap = new HashMap<>();
        for(String[] piece : clothes) {
            String name = piece[0];
            String type = piece[1];
            ArrayList<String> list = clothesMap.getOrDefault(type, new ArrayList<>());
            list.add(name);
            clothesMap.put(type, list);
        }
        // 총 입을 수 있는 조합 갯수 구하기
        // 공식은 해당 타입의 옷의 갯수 + 1(안입는 경우)를 계속 곱해줌
        // 하지만 모두 안입는 경우는 없으므로 -1
        int answer = 1;
        for(String type : clothesMap.keySet()) {
            answer *= (clothesMap.get(type).size() + 1);
        }
        return answer - 1;
    }
}