끄적끄적 코딩
article thumbnail

2021 KAKAO BLIND RECRUITMENT (2021 카카오 블라인드 채용 문제)

course에 주어진 개수만큼의 길이인 메뉴를 만드는 문제입니다.

course = {2, 3, 4}
=> 2개, 3개, 4개의 메뉴를 만들어야합니다.


1. 메뉴는 2사람이상이 주문한 메뉴
2. 같은 길이인 경우 가장 많은 사람들이 주문한 메뉴
3. 같은 길이이면서 사람수도 동일한 경우 전부 메뉴로 만듦
4. 메뉴는 알파벳 순으로 정렬
5. 결과 배열은 알파벳  순으로 정렬


코드 흐름

1. 입력받은 메뉴를 알파벳순으로 정렬합니다. (WX, XW를 비교하기 어려움)
2. 주문 받은 배열의 코스개수에 조건에 맞는 조합과 조합의 수를 추출
3. 추출된 값들을 개수를 기준으로 내림차순 정렬
4. 개수가 2개 이상이면서, 동일한 길이의 더 많은 개수가 없는 경우 결과 배열에 추가
 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

vector<pair<string, int>> orderSet;

bool compare(pair<string, int> a, pair<string, int> b)
{
  if (a.second == b.second)
  {
    return a.first < b.first;
  }
  else
  {
    return a.second > b.second;
  }
}

void checkSet(string order, vector<int> course, string s, int location, int depth)
{
  int index;

  for (int i = 0; i < course.size(); ++i)
  {
    if (course[i] == depth)
    {
      index = orderSet.size();
      for (int k = 0; k < orderSet.size(); ++k)
      {
        if (orderSet[k].first == s)
        {
          index = k;
          break;
        }
      }
      if (index == orderSet.size())
      {
        orderSet.push_back({s, 1});
      }
      else
      {
        ++orderSet[index].second;
      }
    }
  }
  for (int i = location + 1; i < order.length(); ++i)
  {
    checkSet(order, course, s + order[i], i, depth + 1);
  }
}

vector<string> solution(vector<string> orders, vector<int> course)
{
  int len;
  int count[11];
  string s = "";
  vector<string> answer;

  memset(count, 0, sizeof(count));

  for (int i = 0; i < orders.size(); ++i)
  {
    sort(orders[i].begin(), orders[i].end());
  }

  for (int i = 0; i < orders.size(); ++i)
  {
    for (int j = 0; j < orders[i].length(); ++j)
    {
      checkSet(orders[i], course, s + orders[i][j], j, 1);
    }
  }

  sort(orderSet.begin(), orderSet.end(), compare);

  for (int i = 0; i < orderSet.size(); ++i)
  {
    if (orderSet[i].second < 2)
    {
      continue;
    }
    len = orderSet[i].first.length();
    if ((count[len] == 0 || count[len] == orderSet[i].second) && orderSet[i].second != 1)
    {
      answer.push_back(orderSet[i].first);
      count[len] = orderSet[i].second;
    }
  }

  sort(answer.begin(), answer.end());

  return answer;
}

검색 태그