오늘의 알고리즘
[C++]2021 KAKAO BLIND RECRUITMENT 메뉴 리뉴얼(프로그래머스 2레벨)
하늘하늘 .
2022. 2. 14. 16:05
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool cmp(const pair<string, int>& a, const pair<string, int>& b)
{
if (a.second == b.second)
return a.first > b.first;
return a.second > b.second;
}
vector<string> solution(vector<string> orders, vector<int> course)
{
vector<string> answer;
int iCousrseSize = course.size();
int iOrderSize = orders.size();
sort(orders.begin(), orders.end());
for (int i = 0; i < iCousrseSize; i++)
{
// course 갯수에 따른 map
map<string, int> mapString;
for (int j = 0; j < iOrderSize; j++)
{
if (course[i] <= orders[j].size())
{
vector<int> vecInt = {};
// course 갯수에 따른 체크
for (int k = 0; k < course[i]; k++)
{
vecInt.push_back(1);
}
// 최대 갯수에서 coure 갯수를 빼고 난 나머지 체크
for (int k = 0; k < orders[j].size() - course[i]; k++)
{
vecInt.push_back(0);
}
sort(vecInt.begin(), vecInt.end());
do
{
int iVecSize = vecInt.size();
string str = "";
for (int z = 0; z < iVecSize; z++)
{
// 해당 string 추가
if (vecInt[z] == 1)
str += orders[j][z];
}
sort(str.begin(), str.end());
// 맵에 해당 키의 값 증가
mapString[str] += 1;
// 배열 돌리기
} while (next_permutation(vecInt.begin(), vecInt.end()));
}
}
// map에서는 int에따라 sort불가능 따라서 체크
vector<pair<string, int>> vecPair(mapString.begin(), mapString.end());
sort(vecPair.begin(), vecPair.end(), cmp);
int max = 0;
if (!vecPair.empty())
max = vecPair[0].second;
if (max != 1)
{
int iPairSize = vecPair.size();
for ( int j = 0; j < iPairSize; ++j)
{
if (max == vecPair[j].second)
answer.push_back(vecPair[j].first);
else if (max > vecPair[j].second)
break;
}
}
}
sort(answer.begin(), answer.end());
return answer;
}
이런 배열에 좀 많이 약한 거 같다.
맨 위에 맵과 next_permutation을 쓰면 되겠다 했는데 배열을 어떻게 만들까 싶어서 정신 놨던 거 같다.
다른 것보다 이런 문제를 좀 많이 풀어봐야할꺼같다