< 문제링크 >
https://www.welcomekakao.com/learn/courses/30/lessons/42578
< 풀이 >
1. 각 의류를 입는 경우와 입지않는 경우가 있으므로 (의류 수 + 1)이 해당 의류의 경우의 수이다.
2. 의류끼리 조합해서 입으므로 각 경우의 수를 모두 곱해서 누적한다.
3. 모든 의류를 입지 않는 경우를 제외해야 하므로 1을 뺀다.
< 코드 >
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
int solution(vector<vector<string>> clothes) {
unordered_map<string, int> temp;
// 의상종류별로 개수 누적
for (auto name : clothes) {
temp[name[1]]++;
}
// 만약 전체 종류가 1가지 뿐이면 그 개수가 답
if (temp.size() == 1) return temp.begin()->second;
int answer = 1;
// 경우의 수 대로 (각 항목의 수 + 1)을 곱한 것을 누적하여 1을 빼면 답
for (auto i : temp) {
answer *= (i.second + 1);
}
return answer - 1;
}
int main(void)
{
vector<vector<string>> clothes = {
{"yellow_hat", "headgear"},{"blue_sunglasses", "eyewear"},{"green_turban", "headgear"} };
cout << solution(clothes) << endl;
return 0;
}
'알고리즘 > Programmers' 카테고리의 다른 글
Programmers / Stack, Queue / 기능개발 (0) | 2020.09.06 |
---|---|
Programmers / Stack, Queue / 주식가격 (0) | 2020.09.06 |
Programmers / hash / 완주하지 못한 선수 (0) | 2020.09.06 |
Programmers / hash / 전화번호 목록 (0) | 2020.09.06 |
Programmers / hash / 베스트앨범 (0) | 2020.09.06 |