해시 : 프로그래머스 - 위장

phoenixKim·2021년 7월 29일
0

풀이전략

: 어떻게 하지 어떻게 하지 하다가, 일단 노트에다가

1. 나올수 있는 모든 경우의 수를 그려보자.


-> 23개가 나온다.

1번 예제를 표로 그려보면

: 사용안한다라는 표현을 + 1 추가하는 방식으로 생각을 해볼수 있다.
그렇다면 head에는 사용안함, "yellowhat","green_turban"
그러면 eye에는 사용안함, "bluesunglasses" 가 있다.

3 * 2 - 1 이라는 결과를 도출할 수 있고, 이를 나머지 예에 적용하면 일치한다.

2. 동일한 키워드

-> map계열을 사용하자.

소스코드

#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

//06월 13일 04:38 ~ 
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(const auto &i : m)
    {
        answer *= (i.second + 1);
    }
    
    answer -= 1;
    return answer;
}

//0812

#include <string>
#include <vector>
#include <unordered_map>
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++)
    {
        string cloth = clothes[i][1];        
        m[cloth]++;
    }
    
    for(const auto &i  : m)
    {
        answer *= (i.second + 1);
    }
    
    return answer -1 ;
}
profile
🔥🔥🔥

0개의 댓글

관련 채용 정보