문제 풀이/Algorithm

[Programmers] Hash - 위장

soom2628 2020. 7. 8. 10:42

출처: 프로그래머스 - 위장

언어: JAVA

 

 

1. 풀이 과정

이 문제는 스파이가 갖고있는 의상의 종류마다 갯수를 구해서 경우의 수를 구하면 된다.
종류의 갯수는 HashMap을 이용하여 카운트 할 수 있다.

 

2. 주요 코드

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
		HashMap<String, Integer> spy = new HashMap<String, Integer>();
		int answer = 1;

		for (int i = 0; i < clothes.length; i++) {
			if (spy.containsKey(clothes[i][1])) {
				spy.put(clothes[i][1], spy.get(clothes[i][1]) + 1);
			} else {
				spy.put(clothes[i][1], 1);
			}
		}

		for (int v : spy.values()) {
			answer *= v+1;
		}

		return answer - 1;
	}
}

전체 코드