https://school.programmers.co.kr/learn/courses/30/lessons/42578
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr

해시를 이용한 해결책
1. 종류별로 분류
2. 입지 않는 경우 (해당 종류를 쓰지 않는 경우)
3. 전체 조합
즉 해당 종류별로 의상의 개수를 곱해주면 된다.
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
// 옷을 종류별로 구분
unordered_map <string, int> m;
for (vector<string> clothe : clothes) {
m[clothe[1]]++;
}
// 입지 않는 경우 고려, 모든 조합 계산
for (auto it = m.begin(); it != m.end(); it++) {
answer *= it->second + 1;
}
// 모든 경우 - 아무 종류의 옷도 입지 않은 경우 제외
return answer - 1;
}
'콘솔창 & 윈도우창 > 코딩 테스트' 카테고리의 다른 글
프로그래머스 LV.1 신규 아이디 추천 (1) | 2024.11.13 |
---|---|
프로그래머스 LV.2 짝지어 제거 (0) | 2024.11.12 |
프로그래머스 LV.2 영어 끝말잇기 (1) | 2024.11.11 |
프로그래머스 LV.2 소수 찾기 (0) | 2024.11.11 |
프로그래머스 LV.1 완주하지 못한 선수 (0) | 2024.11.10 |