프로그래머스/Lv.2
[C++] 프로그래머스 의상
MINU.SHINNNN
2023. 2. 23. 18:11
https://school.programmers.co.kr/learn/courses/30/lessons/42578
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
1. 해싱을 통해 옷 종류별로 몇 벌씩 가지고 있는지 count 한다.
2. 옷 종류별로, 경우의 수(옷 가지 수 + 안입는경우(1))를 answer에 차례로 곱해주면 모든 경우의 수를 계산할 수 있다.
3. 모든 옷을 입지 않는 경우(1)을 빼주어 답을 구한다.
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 1;
unordered_map<string ,int> m;
for (int i=0; i<clothes.size(); i++){
m[clothes[i][1]]++;
}
for (auto& iter:m){
answer*=(iter.second+1);
}
return --answer;
}