[프로그래머스][C++] 위클리 챌린지 4주차 직업군 추천하기

WestCoast·2021년 8월 23일
0

코딩테스트 풀이

목록 보기
6/13
post-thumbnail

문제

문제 링크 - 직업군 추천하기

풀이

알고리즘

  1. 입력으로 받아준 table의 열을 공백 문자 기준으로 모두 잘라서 재구성.
vector<vector<string>> new_table = TableOrganize(table);


2. new_table, languages, preference를 이용해서 문제가 요구하는 return 값인 '언어 선호도 x 직업군 언어 점수의 총합이 가장 높은 직업군'을 구함.

string answer = FindHighScoreJob(new_table, languages, preference);
  • for (int l = 0; l < languages.size(); l++)
    input으로 주어진 languages중 하나를 잡는다.
    • for (int i = 0; i < new_table.size(); i++)
      new_table의 행(5개 직업군)만큼 반복
      • for (int j = 1; j < new_table[i].size(); j++)
        new_table의 열(5개 언어) 종류만큼 반복
        만약 new_table[i][j]와 languages[l]이 같다면
        'scores[i] += 언어 선호도 x 직업군 언어 점수'

코드

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector<vector<string>> TableOrganize(vector<string> table);
string FindHighScoreJob(vector<vector<string>> new_table, vector<string> languages, vector<int> preference);

string solution(vector<string> table, vector<string> languages, vector<int> preference)
{
    //테이블 재구성
    vector<vector<string>> new_table = TableOrganize(table);

    //가장 높은 점수를 얻은 직업군 탐색
    string answer = FindHighScoreJob(new_table, languages, preference);

    return answer;
}

//{SI, JAVA, JAVASCRIPT, SQL, PYTHON, C#}, ... 공백 기준으로 열을 잘라서 재구성
vector<vector<string>> TableOrganize(vector<string> table)
{
    vector<vector<string>> new_table(5);

    for (int i = 0; i < table.size(); i++)
    {
        string temp = "";
        for (int j = 0; j < table[i].size(); j++)
        {
            if (table[i][j] != ' ')
                temp += table[i][j];
            else
            {
                new_table[i].push_back(temp);
                temp = "";
            }
        }
        new_table[i].push_back(temp);
    }

    return new_table;
}

string FindHighScoreJob(vector<vector<string>> new_table, vector<string> languages, vector<int> preference)
{
    vector<int> scores(5);

    //조사할 언어만큼 반복
    for (int l = 0; l < languages.size(); l++)
        //5개 직업군만큼 반복
        for (int i = 0; i < new_table.size(); i++)
            //5개 언어 종류만큼 반복
            for (int j = 1; j < new_table[i].size(); j++)
                if (new_table[i][j] == languages[l])
                    scores[i] += preference[l] * (6 - j);

    int maxV = *max_element(scores.begin(), scores.end());

    vector<string> temp;
    for (int i = 0; i < 5; i++)
        if (scores[i] == maxV)
            temp.push_back(new_table[i][0]);

    return *min_element(temp.begin(), temp.end());
}

주석 코드

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

vector<string> table = {"SI JAVA JAVASCRIPT SQL PYTHON C#",
                        "CONTENTS JAVASCRIPT JAVA PYTHON SQL C++",
                        "HARDWARE C C++ PYTHON JAVA JAVASCRIPT",
                        "PORTAL JAVA JAVASCRIPT PYTHON KOTLIN PHP",
                        "GAME C++ C# JAVASCRIPT C JAVA"};
vector<string> languages = {"PYTHON", "C++", "SQL"};
vector<int> preference = {7, 5, 5};

vector<vector<string>> TableOrganize(vector<string> table);
string FindHighScoreJob(vector<vector<string>> new_table, vector<string> languages, vector<int> preference);

string solution(vector<string> table, vector<string> languages, vector<int> preference)
{
    //테이블 재구성
    vector<vector<string>> new_table = TableOrganize(table);

    //가장 높은 점수를 얻은 직업군 탐색
    string answer = FindHighScoreJob(new_table, languages, preference);

    return answer;
}

//{SI, JAVA, JAVASCRIPT, SQL, PYTHON, C#}, ... 공백 기준으로 열을 잘라서 재구성
vector<vector<string>> TableOrganize(vector<string> table)
{
    //재구성할 테이블 원소들을 담을 2차원 배열
    vector<vector<string>> new_table(5);

    //table 행 길이만큼 반복
    for (int i = 0; i < table.size(); i++)
    {
        string temp = "";
        //table 열 길이만큼 반복
        for (int j = 0; j < table[i].size(); j++)
        {
            //table[i][j]가 공백이 아니라면
            if (table[i][j] != ' ')
                //temp에 table[i][j]를 더해줌
                temp += table[i][j];
            else
            {
                //table[i][j]가 공백이라면 새로운 테이블에 temp를 넣어주고
                new_table[i].push_back(temp);
                //temp를 비워줌
                temp = "";
            }
        }
        //table[i]의 마지막 원소가 공백이 아니므로 따로 1점짜리 언어를 넣어줌
        new_table[i].push_back(temp);
    }

    return new_table;
}

string FindHighScoreJob(vector<vector<string>> new_table, vector<string> languages, vector<int> preference)
{
    vector<int> scores(5);

    //조사할 언어만큼 반복
    for (int l = 0; l < languages.size(); l++)
        //5개 직업군만큼 반복
        for (int i = 0; i < new_table.size(); i++)
            //5개 언어 종류만큼 반복
            for (int j = 1; j < new_table[i].size(); j++)
                // ex1) new_table[0][4]==languages[0]
                // ex1) PYTHON==PYTHON
                if (new_table[i][j] == languages[l])
                    // ex1) scores[0] += 7 * (6 - 4);
                    scores[i] += preference[l] * (6 - j);

    //maxV = 직업군별 점수 중 가장 높은 점수
    int maxV = *max_element(scores.begin(), scores.end());

    vector<string> temp;
    //가장 높은 점수가 2개 이상일 수 있으므로
    //5개 직업군만큼 반복하며 maxV와 같은 scores[i] 값이 있다면 temp에 저장
    for (int i = 0; i < 5; i++)
        if (scores[i] == maxV)
            temp.push_back(new_table[i][0]);

    //temp에 저장된 값 중 가장 작은 값(사전 순으로 가장 빠른 직업군) return
    return *min_element(temp.begin(), temp.end());
}

int main()
{
    cout << solution(table, languages, preference);
}

여담

왜 3주차보다 쉬워진건지 모르겠다...
위클리 챌린지가 몇 주차까지 나올지 잘 모르겠지만 3주차를 너무 어려운 난이도로 내서 난이도 조절에 들어간 것 같기도 하다.
비교해보자면 2주차와 비슷한 수준의 문제였던 것 같다.

이번 문제는 보기에서 주어진 table과 실제 input으로 주어진 table의 행과 열이 굳이 반대로 되어있어서 살짝 헷갈리는 감이 있는 것을 제외하면 별로 어려운 부분이 없다.

데이터 테이블에서 필요한 정보를 찾아낼 수 있고 문자열만 조금 다룰 수 있다면 누구나 풀 수 있을 듯 싶다.
앞으로 2~3주 정도 더 문제를 봐야 앞으로 어떤 수준의 문제가 나올지 예상이 갈 듯하다.

포스팅하는 오늘 나온 문제인데 벌써 3주차 완료 336명을 간단히 뛰어넘고 4주차 완료는 551명이다.

profile
게임... 만들지 않겠는가..

0개의 댓글