굥뷰를 햡시댜
[Programmers] 위장 본문
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장 | 프로그래머스
programmers.co.kr
해시를 이용해 모든 조합의 수를 구하는 문제
모든 조합의 수는 각 종류를 곱하고 마지막에 1을 빼면 구할 수 있다.
ex)
얼굴 - 동그란 안경, 검정 선글라스
상의 - 파란색 티셔츠
하의 - 청바지
겉옷 - 긴 코트
이렇게 있다고 했을 때 모든 조합의 수는
얼굴(2) * 상의(1) * 하의(1) * 겉옷(1) - 1 = 모든 조합의 수
로 구할 수 있다.
#include <string>
#include <vector>
#include <map>
using namespace std;
map<string, vector<string>> m;
int solution(vector<vector<string>> clothes) {
int answer = 0;
for(int i=0; i<clothes.size(); i++) {
m[clothes[i][1]].push_back(clothes[i][0]);
}
answer = 1;
for(auto it = m.begin(); it!=m.end(); it++) {
answer *= (it->second.size()+1);
}
answer -= 1;
return answer;
}
'알고리즘 문제 풀이 > KaKao 문제 풀이' 카테고리의 다른 글
[Programmers] 전화번호 목록 (0) | 2019.10.11 |
---|---|
[Programmers] 타겟 넘버 (0) | 2019.10.11 |
[Programmers] 네트워크 (0) | 2019.10.11 |
[Programmers] 단어 변환 (0) | 2019.10.11 |
[Programmers] 영어 끝말잇기 (0) | 2019.10.08 |
Comments