[프로그래머스/C++]Lv.2 - 의상

YH J·2023년 9월 20일
0

프로그래머스

목록 보기
150/168

문제 링크

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

내 풀이

종류와 그 종류의 가짓수를 map에 담는다.
모든 경우의 수를 구하면 되는데
공식을 몰라서 찾아보았다.
한 종류의 가짓수에서 안고를 경우를 +1 한다.
전부 곱해서 모든 경우의 수를 구한 뒤 아무것도 안고른 1개를 빼면 된다.

내 코드

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

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

다른 사람의 풀이

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

using namespace std;

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

    unordered_map <string, int> attributes;
    for(int i = 0; i < clothes.size(); i++)
        attributes[clothes[i][1]]++;
    for(auto it = attributes.begin(); it != attributes.end(); it++)
        answer *= (it->second+1);
    answer--;

    return answer;
}

다른 사람의 풀이 해석

unordered_map을 사용하였다.
풀이방식은 같다.

profile
게임 개발자 지망생

0개의 댓글