프로그래머스-의상[자바]

워니·2024년 5월 30일

문제

최소 1개는 입어야 한다고 했으니
모든 옷 종류+1을 각각 곱해준 후 1을 빼면 정답이다
쉽게 맞출 수 있었다 :)

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        
        Map<String, Integer> clothMap = new HashMap<>();
        for(int i = 0; i < clothes.length; i++) {
            String cloth = clothes[i][1];
            if(clothMap.containsKey(cloth)) {
                clothMap.put(cloth, clothMap.get(cloth)+1);
            } else {
                clothMap.put(cloth, 0);
            }
        }
        
        Collection<Integer> values = clothMap.values();
        for(Integer i : values) answer *= (i+2);
        
        return answer-1;
    }
}
profile
매일, 조금씩 나아가는중

0개의 댓글