본문 바로가기
알고리즘 & 자료구조/프로그래머스

위장

by 신재권 2022. 12. 2.
package programmers;

import java.util.HashMap;
import java.util.Map;

public class 위장 {

   public static int solution(String[][] clothes) {
      Map<String, Integer> map = getClothesPartCountMap(clothes);
      // 모든 부위를 착용 안하는 경우의 수 - 1
      return getAnswer(map) -1;
   }

   private static int getAnswer(Map<String, Integer> map) {
      int answer = 1;
      for (String s : map.keySet()) {
         // 착용안하는 경우의 수 = +1
         answer *= (map.get(s)+1);
      }
      return answer;
   }

   private static Map<String, Integer> getClothesPartCountMap(String[][] clothes) {
      Map<String, Integer> map = new HashMap<>();
      for (String[] clothe : clothes) {
         if (map.containsKey(clothe[1])) {
            map.put(clothe[1], map.get(clothe[1]) + 1);
            continue;
         }
         map.put(clothe[1], 1);
      }
      return map;
   }

   public static void main(String[] args) {
      System.out.println(solution(new String[][] {{"yellowhat", "headgear"},
         {"bluesunglasses", "eyewear"}, {"green_turban", "headgear"}}));
      System.out.println(solution(new String[][] {{"crowmask", "face"},
         {"bluesunglasses", "face"}, {"smoky_makeup", "face"}}));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

올바른 괄호의 개수  (0) 2022.12.03
게임 맵 최단거리  (0) 2022.12.02
숫자게임  (0) 2022.11.19
예산  (0) 2022.11.19
가장 큰 수  (0) 2022.11.17