프로그래머스알고리즘
위장
풀스택 개발자
2020. 10. 1. 20:00
#include <string>
#include <vector>
#include<map>
using namespace std;
int solution(vector<vector<string>> clothes) {
int answer = 0;
map<string, int> arr;
int result = 1;
for (int i = 0; i < clothes.size(); i++)
{
arr[clothes[i][1]]++;
}
for (auto p : arr)
{
result = (p.second + 1) * result;
}
answer = result -1;
return answer;
}
1.각 (벡터배열[i][1])key 의 value값을 1씩 증가한다. 즉 각 유형의 옷의 갯수를 구하는 식이다.
2.arr map의 각 value에 1을 더한후 모든 value를 곱한후 1(아무것도 안입은 경우)을 빼면 결과값이 나온다.
test code
#include <string>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main() {
vector<vector<string>> clothes = { {"yellow_hat","headgear"},{"blue_sunglasses", "eyewear"},{"green_turban","headgear"} };
map<string, int> arr;
int result = 1;
for (int i = 0; i < clothes.size(); i++)
{
arr[clothes[i][1]]++;
}
for (auto p : arr)
{
result = (p.second + 1) * result;
}
}