알고리즘/프로그래머스
프로그래머스 위장 (JAVA)
왕구스
2021. 8. 8. 14:43
코딩테스트 연습 > 해시
위장
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr
굉장히 간단한 문제다. 결국 조합의 경우의 수만 따지면 되는데,
이 문제의 경우는 의상의 종류별 갯수 + 1(그 의상을 입지 않는 경우의 수)를 계속 곱해준 다음 최종적으로 1을 빼주면 된다. (아무것도 입지 않는 경우의 수 제외)
import java.util.*;
class Solution {
static Map<String, Integer> map = new HashMap<>();
public int solution(String[][] clothes) {
for(String[] cloth : clothes){
map.merge(cloth[1], 1, (ori, init)->++ori);
}
int answer = 1;
for(String key : map.keySet()){
answer *= map.get(key)+1;
}
return answer-1;
}
}