문제 푼 날짜 : 2021-09-10
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/84325
아래의 생각대로 코드를 구현하였다.
- 언어의 선호도를 map을 이용하여 저장해준다.
- 각 직업군별로 언어들의 점수와 해당 언어의 선호도를 곱한 후 다 더해준다.
- 가장 큰 점수를 얻은 직업군을 찾아준다.
#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;
}
기본적인 구현능력을 기르기 위해서 난이도가 낮은 문제도 많이 풀어보자.