[프로그래머스] 4주차_직업군 추천하기

박개발·2021년 9월 19일
0

프로그래머스

목록 보기
37/42

문제 푼 날짜 : 2021-09-10

문제

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/84325

접근 및 풀이

아래의 생각대로 코드를 구현하였다.

  1. 언어의 선호도를 map을 이용하여 저장해준다.
  2. 각 직업군별로 언어들의 점수와 해당 언어의 선호도를 곱한 후 다 더해준다.
  3. 가장 큰 점수를 얻은 직업군을 찾아준다.

코드

#include <string>
#include <vector>
#include <map>
#include <sstream>

using namespace std;

string solution(vector<string> table, vector<string> languages, vector<int> preference) {
    string answer = "";
    map<string, int> m;
    int maxScore = -1;
    
    for (int i = 0; i < languages.size(); i++) {
        m[languages[i]] = preference[i];
    }
    
    for (int i = 0; i < table.size(); i++) {
        stringstream ss(table[i]);
        string lang, a, b, c, d, e;
        ss >> lang >> a >> b >> c >> d >> e;
        
        int total = 0;
        
        string tmp[5] = { a, b, c, d, e };
        for (int i = 0; i < 5; i++) {
            if (m.find(tmp[i]) != m.end()) {
                total += m[tmp[i]] * (5 - i);
            }
        }
        if (maxScore < total) {
            maxScore = total;
            answer = lang;
        } else if (maxScore == total) {
            answer = (answer < lang) ? answer : lang;
        }
    }
    return answer;
}

결과

피드백

기본적인 구현능력을 기르기 위해서 난이도가 낮은 문제도 많이 풀어보자.

profile
개발을 잘하고 싶은 사람

0개의 댓글

관련 채용 정보