본문 바로가기
알고리즘/프로그래머스

[ 프로그래머스 / Map ] 위장

by 뎁꼼 2020. 6. 30.

1. 문제


 

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

 

2. 소스코드


- 옷의 카테고리별로 map을 만든 뒤, 카테고리별 갯수를 저장

- 가능한 경우의 수는, 해당 카테고리별 옷의 개수 를 전부 곱해주고 - 1이다.

- -1을 하는 이유는 아무것도 안입는 경우의 수이기 때문.

 

소스코드

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

int solution(vector<vector<string>> clothes) {

    unordered_map<string, int> map;
    int answer = 1;
    vector<string> ctg;
    for(auto cur : clothes){
        if(map[cur[1]] == 0) ctg.push_back(cur[1]);
        map[cur[1]]++;
    }
    for(string str : ctg){
        answer *= map[str] + 1;
    }
    return answer - 1;
}

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