홍시홍의 프로그래밍

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

알고리즘 문제풀이/프로그래머스

[프로그래머스] 위장

홍시홍 2020. 3. 17. 23:02

요구사항

옷의 이름과 종류가 주어졌을때 이를 가지고 조합 구현하기

 

풀이

1, 2, 3이라는 종류의 옷이 3개, 4개, 5개 있을때 겹치지 않고 입을 수 있는 경우의 수는 3*4*5이다

이중에서 아무것도 안입은 수는 빼야하므로 -1을 하면 된다

 

아이디어가 중요한 문제인거 같다

 

#include <string>
#include <vector>
#include <iostream>
#include <string.h>
#include <map>
using namespace std;


map<string, int> m;
int a[31];
int solution(vector<vector<string>> clothes) {
	int answer = 0;
	for (int i = 0; i < clothes.size(); i++) m[clothes[i][1]]++;
	int cnt = 0;
	for (auto iter = m.begin(); iter != m.end(); iter++) {
		a[cnt] = iter->second;
		cnt++;
	}
	if (cnt == 1) answer = a[0];
	else {
		answer = 1;
		for (int i = 0; i < cnt; i++) {
			answer *= (a[i]+1);
		}
		answer -= 1;
	}

	return answer;
}
Comments