가티있는블로그

[프로그래머스] 위장문제

2019. 10. 28. 20:24 | 프로그래밍/코딩테스트 문제

프로그래머스에서 위장문제 문제를 해결했다.

C++의 map 을 사용하여 문제를 해결하였다. map 에 관한 내용은 블로그를 통해 학습하고 참고하여 사용하였다.

처음에 조합의 개수를 이상하게 생각해서 계산을 잘못하였는데 블로그에서 설명된 내용을 보며 조합의 개수를 맞게 구하여 문제를 해결 할 수 있었다.

  • clothes_list  map을 이용하여 옷의 종류와 개수를 넣었다.
  • 종류+1 한 값을 전부 곱한 다음 1을 빼어 결과를 계산하였다. 이에 대한 자세한 내용은 위에 조합 개수에 참고하였다고 한 블로그에 자세히 설명되어 있다.
#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(vector<vector<string>> clothes) {
	int answer = 0;
	map<string, int> clothes_list;
	for (int i = 0; i < clothes.size(); i++) {
		auto itr = clothes_list.find(clothes[i][1]);
		if (itr != clothes_list.end()) {
			//존재할 경우 개수 증가
			itr->second++;
		}
		//존재하지 않을 경우 새로 추가
		else {
			clothes_list.insert(make_pair(clothes[i][1], 1));
		}
	}

	auto itr = clothes_list.begin();
	answer = itr->second + 1;
	itr++;
	for (; itr != clothes_list.end(); itr++) {
		int r = itr->second + 1;
		answer *= r;
	}

	answer--;
	return answer;
}