Study
프로그래머스-메뉴리뉴얼 본문
코딩테스트 연습 - 메뉴 리뉴얼
레스토랑을 운영하던 스카피는 코로나19로 인한 불경기를 극복하고자 메뉴를 새로 구성하려고 고민하고 있습니다. 기존에는 단품으로만 제공하던 메뉴를 조합해서 코스요리 형태로 재구성해서
programmers.co.kr
<풀이>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool cmp(pair<string, int>a, pair<string, int> b){
return a.second > b.second;
}
void DFS(map<string, int>& dic, string& order, string tmp, int index, int depth){
if(depth == tmp.length()){
dic[tmp]++;
return;
}
for(int i = index; i<order.length(); i++){
tmp[depth] = order[i];
DFS(dic,order,tmp,i+1,depth+1);
}
}
vector<string> solution(vector<string> orders, vector<int> course) {
vector<string> answer;
map<string, int> dic;
for(int i=0; i<orders.size(); i++){
sort(orders[i].begin(), orders[i].end());
for(int j=0; j<course.size(); j++){
string tmp = "";
tmp.resize(course[j]);
DFS(dic,orders[i], tmp,0,0);
}
}
vector<pair<string,int>> sorted;
for(auto& order : dic)//dic에서 차례대로 객체를 꺼내서 order에 넣는다
if(order.second > 1)
sorted.push_back({order.first,order.second});
sort(sorted.begin(), sorted.end(),cmp);
for(int i=0; i<course.size(); i++){
int max = 0;
for(int j=0; j<sorted.size(); j++){
if(sorted[j].first.length() != course[i])
continue;
else if(max == 0){
answer.push_back(sorted[j].first);
max = sorted[j].second;
}
else if (max == sorted[j].second)
answer.push_back(sorted[j].first);
else
break;
}
}
sort(answer.begin(), answer.end());
return answer;
}
-참고한 사이트
'알고리즘 > C++ 문제풀이' 카테고리의 다른 글
프로그래머스-오픈채팅방 (0) | 2021.07.22 |
---|---|
프로그래머스-타겟넘버 (0) | 2021.07.20 |
프로그래머스-숫자의 표현 (0) | 2021.07.15 |
프로그래머스-크레인 인형뽑기 (0) | 2021.07.14 |
프로그래머스 -짝지어 제거하기 (0) | 2021.07.13 |