[Programmers] 해시 - 위장

zzenee·2022년 9월 23일
0

Algorithm&Coding-test

목록 보기
22/30
post-thumbnail

https://school.programmers.co.kr/learn/courses/30/lessons/42578

Problem

Code

import java.util.*;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        HashMap<String, Integer> hmap = new HashMap<>();
        for (int i=0; i<clothes.length; i++) {
            hmap.put(clothes[i][1], hmap.getOrDefault(clothes[i][1], 0) + 1);
        }
        
        for (String key : hmap.keySet()) {
            answer *= (hmap.get(key) + 1);
        }
        answer--; // 아무것도 입지 않는 경우
    
        return answer;
    }
}

Result

Note

A그룹의 원소가 a개, B그룹의 원소가 b개 있으면 조합의 수는 ab
-> A그룹에서만 선택하거나 B그룹에서만 선택하는 경우에는 (a+1)
(b+1)

profile
꾸준히

0개의 댓글