<Programmers> Hash_위장 c++

Google 아니고 Joogle·2022년 1월 12일
0

Programmers

목록 보기
12/22
post-thumbnail

이 문제는 경우의 수만 제대로 알면 풀 수 있는 문제다

예를 들어 스파이가 가지고 있는 의상의 종류의 개수가
headgear=2, eyewear=3, face=4 이렇게 있을 때

headgear 1번 선택, headgear 2번 선택, 선택하지 않음 총 3개가 있다
그러니까 각각의 경우 +1개 만큼 선택의 수가 있다.

모든 경우의 수는 (2+1) * (3+1) * (4+1)이다. 하지만 문제에서 스파이는 하루에 최소 한 개의 의상은 입는다고 했기 때문에 아무것도 입지 않는 경우의 수 1 을 빼준다.

1)unordered_map <string, int> myclothes;을 선언하고 myclothesclothes[i][1]값들 즉 headgear, eyewear, ...넣으며 넣을 때마다 value를 증가시킨다 (몇 개가 있는지 check)

    for (auto item: clothes) 
        myclothes[item[1]]++;

2) myclothes의 value값 (즉 second)에 1을 더한 후 곱해 나간다.
(마지막 return 할 때 -1을 해준다)

    for (auto tmp: myclothes)
        answer*=(tmp.second+1);

소스코드

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

using namespace std;

int solution(vector<vector<string>> clothes) {
    int answer = 1;

    unordered_map <string, int> myclothes;
    for (auto item: clothes) 
        myclothes[item[1]]++;
    for (auto tmp: myclothes)
        answer*=(tmp.second+1);
 
    return answer-1;
}

profile
Backend 개발자 지망생

0개의 댓글