본문 바로가기

Development/CodingTest

[Hash]프로그래머스 level 2 의상 java 풀이

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 접근

  1. 각 옷의 유형을 Key, 종류를 Value로 하고
  2. 모든 경우의 수를 계산한다.
  3. 아무 것도 안 입었을 때를 answer - 1, 옷 종류 당 한 번씩 안 입은 경우를 +1 한다.

코드

import java.util.HashMap;

class Solution {
    public int solution(String[][] clothes) {
        HashMap<String, Integer> clothTable = new HashMap<>();
        for (String[] cloth : clothes) {
            String type = cloth[1];
            clothTable.put(type, clothTable.getOrDefault(type, 0) + 1);
        }

        int answer = 1;
        for (Integer integer : clothTable.values()) {
            answer *= integer + 1;
        }
        return answer - 1;
    }
}