글 작성자: Doublsb

https://programmers.co.kr/learn/courses/30/lessons/84325

 

풀이

- 사용 언어와 선호도를 입력하면 적절한 직군을 알려 주는 프로그램

- 주어진 table을 잘 split하는 게 핵심인듯

- 총합이 같은 직군이면 알파벳순으로 빠른 것을 출력한다

 

- 알파벳순은 SortedDictionary로 해결해야겠다

- 또다시 푸는 방법을 알려준 문제라 큰 고민은 없음

 

C#

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public string solution(string[] table, string[] languages, int[] preference) {
        var DB = new DB(table);
        string answer = DB.Find(languages, preference);
        
        return answer;
    }
}

public class DB {
    private Dictionary<string, Tech> data;
    
    public DB(string[] table){
        data = new Dictionary<string, Tech>();
        
        int pos = 0;
        for(int i=0; i<table.Length; i++) { 
            pos = table[i].IndexOf(' ');
            data.Add(table[i].Substring(0, pos), new Tech(table[i].Substring(pos + 1)));
            
        }
    }
    
    public string Find(string[] lang, int[] prefer){
        var score = new SortedDictionary<string, int>();
        foreach(var key in data.Keys) { 
            
            score.Add(key, 0);
            
            for(int i=0; i<lang.Length; i++) {
                score[key] += data[key].Find(lang[i]) * prefer[i];
            }
        }

        return score.First(e => e.Value == score.Values.Max()).Key;
        
    }
}

public class Tech {
    private Dictionary<string, int> score;
    
    public Tech(string str){
        score = new Dictionary<string, int>();
        
        var strs = str.Split(' ');
        for(int i=0; i<strs.Length; i++){
            score.Add(strs[i], 5-i);
        }
    }

    public int Find(string language) {
        if(score.ContainsKey(language)) return score[language];
        else return 0;
    }
}

 

결과

100.0 / 100.0

 

피드백

-

반응형