Algorithms/- 프로그래머스

프로그래머스 - 위장 [자바]

자굿 2022. 3. 3. 22:51
 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

정답(Solution)

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        int len = clothes.length;

        Map<String, Integer> siMap = new HashMap<>();

        for(int i=0; i<len; i++){
            siMap.put(clothes[i][1], siMap.getOrDefault(clothes[i][1], 0) + 1);
        }

        for(int num : siMap.values()){
            answer = answer * (num + 1);
        }

        return answer - 1;
    }
}

 

분석

  • 자기 자신을 포함하지 않는 경우를 생각해서 num + 1
  • 모두 자기 자신을 빼서 선택하지 않는 경우를 answer - 1
반응형