311. 직업군 추천하기

아현·2021년 10월 7일
0

Algorithm

목록 보기
333/400
post-custom-banner

프로그래머스




1. 시뮬레이션


import sys
from collections import defaultdict
def solution(table, languages, preference):
    answer = 0;
    si = defaultdict(int)
    contents = defaultdict(int)
    hardware = defaultdict(int)
    portal = defaultdict(int)
    game = defaultdict(int)

    for t in table:
        lst = t.split(' ')
        if lst[0] == "SI":
            for i in range(1, len(lst)):
                si[lst[i]] = 6 - i;
        elif lst[0] == "CONTENTS":
            for i in range(1, len(lst)):
                contents[lst[i]] = 6 - i;
        elif lst[0] == "HARDWARE":
            for i in range(1, len(lst)):
                hardware[lst[i]] = 6 - i;
        elif lst[0] == "PORTAL":
            for i in range(1, len(lst)):
                portal[lst[i]] = 6 - i;
        elif lst[0] == "GAME":
            for i in range(1, len(lst)):
                game[lst[i]] = 6 - i;
    
    
    best = -sys.maxsize
    d = {
        "SI": si,
        "CONTENTS": contents, 
        "HARDWARE": hardware, 
        "PORTAL":portal, 
        "GAME": game
    }
    
    keep = []
    for name, dict in d.items():
        sum = 0
        for i in range(len(languages)):
            sum += dict[languages[i]] * preference[i]
        
        if sum > best:
            best = sum
            answer = name
            keep = []
        elif sum == best:
            keep.append(answer)
            keep.append(name)
            
    if len(keep):
        keep.sort()
        answer = keep[0]
    return answer



2. JavaScript


function solution(table, languages, preference) {
    
    table = table.map((r) => r.split(' '))
                .map((t) => [...t.splice(0, 1), t]) //  [ 'SI', [ 'JAVA', 'JAVASCRIPT', 'SQL', 'PYTHON', 'C#' ] ]
                .map(([t, arr]) => [t,
                     languages.reduce((acc, l, i)=> {
                        acc += (5 - (arr.indexOf(l) === -1 ? 5 : arr.indexOf(l)))
                                    * preference[i];
                        return acc;
                     }, 0)]) // [ 'SI', 55 ]
                .sort((a, b) => b[1] - a[1] - (a[0] < b[0]))[0][0]
                 
               
                
    //console.log(table);
    
    return table;
}



3. C++



#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

string solution(vector<string> table, vector<string> languages, vector<int> preference) {
    vector<int>score(5, 0);
    vector<vector<string>>score_table(5, vector<string>(0, ""));
    vector<string>name = { "SI", "CONTENTS", "HARDWARE", "PORTAL", "GAME" };

    int i = 0;
    int j = 0;
    int cnt = 0;
    string buffer = "";

    for (i = 0; i < 5; ++i) {
        stringstream ss(table[i]);
        int cnt = 0;
        while (ss >> buffer) {
            //cout << buffer << endl;
            for (j = 0; j < languages.size(); ++j) {
                if (buffer == languages[j]) {
                    score[i] += (preference[j] * (6 - cnt));
                }
            }
            cnt++;
        }
    }
    
    int max_score = 0;
    int max_idx = 0;
    for (i = 0; i < 5; ++i) {
        if (score[i] > max_score) {
            max_score = score[i];
            max_idx = i;
        }
        else if (score[i] == max_score)
            if (name[i].compare(name[max_idx]) < 0)
                max_idx = i;
    }

    return name[max_idx];
}

profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글