# 생각
map을 이용하여 종류별로 카운트를 해주고
선택하지 않는 경우의 수도 존재하므로 각 종류의 개수 + 1을 곱해주면 전체 경우의 수를 얻어낼 수 있다
예를 들어, 상의 2가지, 하의 1가지가 존재한다면
선택하지 않는 경우까지 포함하여 3 * 2 = 6 가지의 경우의 수가 나온다
단, 문제의 조건에서 둘다 선택하지 않는 경우는 없으므로 -1을 해줘야 한다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
map<string, int> m;
for (auto& type : clothes) m[type[1]]++;
for (auto iter = m.begin(); iter != m.end(); iter++) answer *= iter->second + 1;
return answer - 1;
}
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Lv.2] 타겟 넘버 with cpp (0) | 2022.03.02 |
---|---|
[Lv.3] 베스트앨범 with cpp (0) | 2022.03.01 |
[Lv.2] 전화번호 목록 with cpp (0) | 2022.02.28 |
[Lv.1] 완주하지 못한 선수 with cpp (0) | 2022.02.28 |
[Lv.1] 크레인 인형뽑기 게임 with cpp (0) | 2022.02.20 |