본문 바로가기

c++/프로그래머스

프로그래머스 : 위장 C++

https://programmers.co.kr/learn/courses/30/lessons/42578

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

풀이

1. 모든 종류의 옷을 한벌씩 입어야되는것이 아닌, 단 한개의 옷이라도 입으면 되는 것이 핵심!

 

2. 종류별로 옷의 갯수를 map을 활용해서 정리

 

3. 각 옷을 안입는 경우를 포함하기 위해서, 경우의 수를 구할때 각 옷종류의 갯수마다 +1

 

4. 모든 옷을 안입는 경우 한가지를 최종적으로 -1

 

 

#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(vector<vector<string>> clothes) {
    
    int answer = 1;
    
    map <string,int> m;
    
    for(int i=0;i<clothes.size();i++)
    {
        string key = clothes[i][1];
        m[key]++;
    }
    
    for(auto data : m)
    {
        answer *= (data.second+1);
    }
    
    return answer-1;
}