코딩테스트
[C++, 코딩테스트] 프로그래머스 : 의상 with 해시
Hayden_
2024. 2. 16. 21:35
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
map<string, int> collector;
for (vector<string> cloth : clothes) {
collector[cloth[1]]++;
}
for (auto &value : collector){
answer *= value.second + 1;
}
return answer - 1;
}
사실 쉬운 문제인데, 어떻게 풀어야할지 헷갈렸었다. 그런 문제는 보면 조합으로 모든 경우의 수를 찾아봐야하나? 이런 생각도 들고 그랬지만 특정 케이스를 찾는 것이 아닌, 경우의 수만 구하면 되기 때문에 의상 수에 +1을 해서 다 곱하고 -1을 하면 아무것도 안입은 경우를 뺀 모든 경우의 수를 구할 수 있다. +1을 하는 이유는 안입은 경우의 수를 넣는 것이다.