1. 문제
코딩테스트 연습 - 위장
programmers.co.kr
2. 소스코드
- 옷의 카테고리별로 map을 만든 뒤, 카테고리별 갯수를 저장
- 가능한 경우의 수는, 해당 카테고리별 옷의 개수 를 전부 곱해주고 - 1이다.
- -1을 하는 이유는 아무것도 안입는 경우의 수이기 때문.
소스코드
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
int solution(vector<vector<string>> clothes) {
unordered_map<string, int> map;
int answer = 1;
vector<string> ctg;
for(auto cur : clothes){
if(map[cur[1]] == 0) ctg.push_back(cur[1]);
map[cur[1]]++;
}
for(string str : ctg){
answer *= map[str] + 1;
}
return answer - 1;
}
https://programmers.co.kr/learn/courses/30/lessons/42578#
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[ 프로그래머스 / 우선순위큐 ] 라면공장 (0) | 2020.06.30 |
---|---|
[ 프로그래머스 / 완전탐색 ] 카펫 (0) | 2020.06.30 |
[ 프로그래머스 / DFS ] 타겟 넘버 (0) | 2020.06.30 |
[ 프로그래머스 / 투포인트 ] 숫자의 표현 (0) | 2020.06.30 |
[ 프로그래머스 / DP ] 땅따먹기 (0) | 2020.06.30 |