알고리즘/프로그래머스
[프로그래머스] 위장_자바
Ellie67
2022. 11. 26. 20:34
https://school.programmers.co.kr/learn/courses/30/lessons/42578
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
예를 들어 상의가 A벌, 하의가 B벌, 안경이 C벌 있다고 하자.
상의, 하의, 안경을 조합한 경우의 수는 A*B*C 이다.
하지만 각 의상을 선택 안 하는 경우도 있기 때문에 경우의 수는 (A+1)*(B+1)*(C+1) 이다.
여기에 모두 선택 안 하는 경우를 뺀다.
import java.util.*;
public class P4 {
public static void main(String[] args){
P4 p = new P4();
String[][] c = {{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"}, {"green_turban", "headgear"}};
System.out.println(p.solution(c));
}
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(String x : map.keySet()){
answer *= (map.get(x)+1);
}
answer -= 1;
return answer;
}
}