https://school.programmers.co.kr/learn/courses/30/lessons/42578
종류와 그 종류의 가짓수를 map에 담는다.
모든 경우의 수를 구하면 되는데
공식을 몰라서 찾아보았다.
한 종류의 가짓수에서 안고를 경우를 +1 한다.
전부 곱해서 모든 경우의 수를 구한 뒤 아무것도 안고른 1개를 빼면 된다.
#include <string>
#include <vector>
#include <map>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
map<string, int> m;
for(vector<string> c : clothes)
m[c[1]]++;
for(auto num : m)
answer *= num.second + 1;
return answer - 1;
}
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
unordered_map <string, int> attributes;
for(int i = 0; i < clothes.size(); i++)
attributes[clothes[i][1]]++;
for(auto it = attributes.begin(); it != attributes.end(); it++)
answer *= (it->second+1);
answer--;
return answer;
}
unordered_map을 사용하였다.
풀이방식은 같다.