[프로그래머스] 위장 자바(Java)
본문 바로가기
알고리즘 풀이

[프로그래머스] 위장 자바(Java)

by IYK2h 2023. 4. 3.
728x90

풀이

  • 조합의 경우의 수는 A * B

코드

import java.util.*;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        HashMap<String, Integer> map = new HashMap<>();
        
        // 종류별 옷 카운트
        for(int i =0; i<clothes.length; i++){
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1],0)+1);
        }
        
        Iterator<Integer> it = map.values().iterator();
        // 조합의 경우의 수
        while(it.hasNext()) {
            // + 1 는 하나만 선택 가능 하기 때문
            answer *= it.next().intValue()+1;
        }
        // 아무것도 입지 않는 수는 없다.
        return answer-1;
    }
}
728x90

댓글