728x90

[문제]

https://programmers.co.kr/learn/courses/30/lessons/42578?language=java#

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

[문제 풀이]

- 얼굴: 동그란 안경, 검정 선글라스, none => 3가지의 경우의 수

- 상의: 파란색 티셔츠, none  => 2가지의 경우의 수

- 하의: 청바지, none => 2가지의 경우의 수

- 겉옷: 긴코트, none => 2가지의 경우의 수

 

총 3 * 2 * 2* 2 = 24가지가 나올 수 있다.

그런데 최소 한개의 옷을 입여야 하기 때문에 [none, none, none, none]을 고른 경우의 수 1개를 빼주어야 한다.

따라서 23가지

 

[코드]

public static int solution(String[][] clothes) {
  Map<String, Integer> hm = new HashMap<>();
  for (int i=0; i<clothes.length; i++) {
  	hm.put(clothes[i][1], hm.getOrDefault(clothes[i][1], 1) + 1);
  }
    
  int ans = 1;
  for (Integer value : hm.values()) {
  	ans *= value;
  }
  
  return ans-1;
}
728x90

+ Recent posts