프로그래머스 문제풀이/LEVEL 2

[프로그래머스 / Level 2] 위장

지나가던 개발자 2020. 9. 21. 22:52
반응형

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

 

코딩테스트 연습 - 위장

 

programmers.co.kr

문제 접근법

  • map을 이용해서 카테고리의 갯수를 파악합니다.
  • 전체의 조합의 갯수를 구하는 방법은 각 카테고리의 갯수 + 입지 않는 경우의 수를 더해서 모두 곱해주면 됩니다.
  • 단, 옷을 하나도 안 입는 경우가 없으므로 -1을 해주어야 합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <vector>
#include <map>
 
using namespace std;
 
int solution(vector<vector<string>> clothes) {
    int answer = 1;
    int len = clothes.size();
    
    map<stringint> m;
    
    for(int i = 0; i < len; i++)
    {
        m[clothes[i][1]]++;
    }
    
    map<stringint>::iterator iter;
    
    for(iter = m.begin(); iter != m.end(); ++iter)
    {
        answer *= (iter->second + 1);
    }
 
    return answer - 1;
}
cs

 

반응형