Development Logs/Algorithms
[JAVA] 프로그래머스: 위장 (코딩테스트 고득점 kit > 해쉬)
유뱅유뱅뱅
2020. 7. 13. 20:09
반응형
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr
수학적인 접근이 필요한 문제임
의상 종류의 수와 종류들의 각 갯수를 가지고 풀 수 있는 문제임
해쉬를 이용해서 각 의상 종류의 갯수들을 저장해줌
그리고 경우의 수를 구해줌
* for(String key : keys) 는 keys에 있는 값들을 key값에 넣는 것이다.
(keys에 값이 더이상 없을때까지)
import java.util.*;
//위장
// [의상의 이름, 의상의 종류]
// 최소 한 개 이상의 의상
// 서로 다른 옷의 조합의 수 return
// 같은 종류의 의상은 조합 불가
class Solution {
public int solution(String[][] clothes) {
int answer = 0;
// 의상 종류끼리 갯수 저장
Map<String, Integer> clothesKindsMap = new HashMap<>();
String clothesKindsName;
for(int i=0; i<clothes.length; i++){
clothesKindsName = clothes[i][1];
if(clothesKindsMap.containsKey(clothesKindsName)){
clothesKindsMap.put(clothesKindsName, clothesKindsMap.get(clothesKindsName) + 1);
}
else{
clothesKindsMap.put(clothesKindsName, 1);
}
}
// 경우의 수 계산
Set<String> keys = clothesKindsMap.keySet();
int temp;
answer = 1;
for(String key : keys){
// 해당 의상 종류 갯수(안 입는 경우 포함)
temp = clothesKindsMap.get(key) + 1;
answer *= temp;
}
// 아무 것도 안 입은 경우 제외
answer--;
return answer;
}
}
반응형