[문제 링크]

 

코딩테스트 연습 - 위장

 

programmers.co.kr


input 으로 vector<vector<string>> clothes 가 주어진다.

vector에서 clothes[N][0] 은 옷 이름을 나타내고, clothes[N][1] 은 옷 종류를 나타내는 것을 알 수 있다.

 

필자는 map<string, int> 컨테이너에 입력받은 옷 종류를 key값으로 하여 개수를 카운팅해주었다.

 

조합의 수를 구하는 방법은 옷 종류 A, B, C가 있다고 했을 때, 입지 않는 경우도 존재하기 때문에

(A의 개수 + 1) * (B의 개수 + 1) * (C의 개수 + 1) - 1 이 된다.

여기서 마지막에 -1 하는 이유는 아무 옷도 입지 않은 경우를 포함하면 안되기 때문에 빼준 것이다.


#include <string>
#include <vector>
#include <map>

using namespace std;

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

	map<string, int> clothesCnt;
	map<string, int>::iterator iter;

	for (int i = 0; i < clothes.size(); i++)
		clothesCnt[clothes[i][1]] += 1;

	iter = clothesCnt.begin();

	while (iter != clothesCnt.end())
	{
		answer *= (iter->second + 1);
		iter++;
	}

	answer -= 1;

	return answer;
}

알고리즘 200일 프로젝트 - 135 day

+ Recent posts