Home [프로그래머스] 위장
Post
Cancel

[프로그래머스] 위장

[프로그래머스] 위장

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

풀이방법

  • HashMap을 이용해 풀이
  • 다른 옷들을 입을 조합의 개수를 알아야 함
    • 예시1 설명
      • headgear 종류에서 의상 이름은 2가지이며 yellow_hat, green_turban, X 3가지의 경우의 수 존재
      • eyewear 종류에서 의상 이름은 1가지이며 bluesunglasses, X 2가지의 경우의 수 존재
      • 결국 총 경우의 수는 3 * 2 = 6가지
      • 그러나 문제 조건에 최소 1개의 의상은 입는다고 하였으므로 둘 다 입지 않은 X는 빼야하므로 -1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.HashMap;
import java.util.Map;

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

        Map<String, Integer> map = new HashMap<>();
        for(int i=0; i<clothes.length; i++) {
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
        }

        for(Map.Entry<String, Integer> entry : map.entrySet()) {
            answer *= (entry.getValue() + 1);
        }

        return answer - 1;
    }
}
This post is licensed under CC BY 4.0 by the author.

Generics

[프로그래머스] 기능개발

Comments powered by Disqus.