📍 문제
📍 풀이
- map / set 활용
- map에 삽입할 때,
- key 값 : 옷의 종류,
- value 값 : 종류 별 옷의 개수
옷의 종류가 이미 있다면 map의 value 값 변경 (1씩 추가해 줌)
없으면 map에 삽입
[(A종류 옷의 수+1) * (B종류 옷의 수+1) * (C종류 옷의 수 +1) ... ] - 1
-1을 하는 이유 : 옷을 아무것도 입지 않는 경우
각 종류 옷의 수에 +1 을 하는 이유 : 그 종류의 옷을 입지 않는 경우를 포함하기 위함임
📍 코드
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
map<string, int> m;
map<string, int>::iterator iter;
for (int i{ 0 }; i < clothes.size(); i++) {
// 옷 종류가 이미 있으면 map의 value 값 변경
if (m.find(clothes[i][1]) != m.end()) {
m[clothes[i][1]] += 1;
}
// 없으면 map에 삽입
else {
m.insert({ clothes[i][1], 1 });
}
}
for (iter = m.begin(); iter != m.end(); iter++) {
answer *= (iter->second + 1);
}
return answer - 1;
}