[해시] 위장

yyeahh·2020년 8월 9일
0

프로그래머스

목록 보기
9/35

|| 문제설명 ||

  1. 스파이들은 매일 다른 옷을 조합하여 입어 자신을 위장한다.
  2. 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성하라.
  • clothes : 스파이가 가진 의상들이 담긴 2차원 배열
    (vector<vector> clothes)
_ clothes의 각 행 = [의상의 이름, 의상의 종류], 문자열
_ 스파이가 가진 의상의 수 : 1개 이상 30개 이하
_ 같은 이름을 가진 의상은 존재하지 않는다.
_ 모든 문자열의 길이 : 1 이상 20 이하, 자연수 (알파벳 소문자 또는 '_')
_ 하루에 최소 한 개의 의상

|| 문제해결과정 ||

- map<string, int> 변수 선언 (의상의 종류, 개수)
- 변수의 각 value에 안입을 경우를 포함하여 answer에 곱하고 하나도 안입을 경우를 제외해준다.

|| 코드 ||

[2020.08.09] 성공
#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(vector<vector<string>> clothes) {
    int answer = 1;
    map<string, int> cnt;
  
    for(int i = 0; i < clothes.size(); i++) {
        cnt[clothes[i][1]]++;
    }
    for(auto i : cnt) {
        answer *= (i.second+1);
    }
    return answer-1;
}
map의 key와 value는 각각 first, second로 접근할 수 있다.


[2021.01.23]
#include <string>
#include <vector>
#include <map>

using namespace std;

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

0개의 댓글