๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 4์ฃผ์ฐจ ์ง์—…๊ตฐ ์ถ”์ฒœํ•˜๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 8. 28.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 4์ฃผ์ฐจ

๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์™€ ์–ธ์–ด ์„ ํ˜ธ๋„๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ์— ๋งž๋Š” ์ง์—…๊ตฐ์„ ์ถ”์ฒœํ•ด์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ํ‘œ๋Š” 5๊ฐœ ์ง์—…๊ตฐ ๋ณ„๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” 5๊ฐœ ์–ธ์–ด์— ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋ฅผ ๋ถ€

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” Hashtable์„ ์ด์šฉํ•ด์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

1. ์ด์ค‘ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ์ง์—…๊ตฐ์— ๋”ฐ๋ฅธ ์–ธ์–ด๋ณ„ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

table์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์ค๋‹ˆ๋‹ค.

 

0๋ฒˆ์งธ๋Š” ์ง์—…๊ตฐ์˜ ์ด๋ฆ„์ด๋ฏ€๋กœ ํ‚ค๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

์–ธ์–ด๋ณ„๋กœ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•ด์ฃผ๋Š”๋ฐ 1๋ฒˆ์งธ๋Š” 5,2๋ฒˆ์งธ๋Š” 4,3๋ฒˆ์งธ๋Š” 3... ์ด๋ฏ€๋กœ 6 - i๋กœ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

[์ง์—…๊ตฐ:[์–ธ์–ด:์ ์ˆ˜]] <- ์ด๋Ÿฐ์‹์œผ๋กœ 2์ค‘ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

func setJobLanguageScore(table:[String]) -> [String:[String:Int]] {
    var jobs:[String:[String:Int]] = [:]
    table.forEach {
        let split = $0.split(separator: " ")
        let job = String(split[0])
        for (i,lang) in split.enumerated() {
            if i == 0 {
                jobs[job] = [:]
                continue
            }
            jobs[job]![String(lang)] = 6-i
        }
    }
    return jobs
}

 

2. ์ง์—…๊ตฐ๋ณ„ ์–ธ์–ด ์ ์ˆ˜ * ์–ธ์–ด์„ ํ˜ธ๋„์˜ ํ•ฉ์„ ๊ตฌํ•ด์ค€๋‹ค.

 

์œ„์—์„œ ๊ตฌํ•œ ์ง์—…๊ตฐ์— ๋”ฐ๋ฅธ ์–ธ์–ด๋ณ„ ์ ์ˆ˜ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ’์— ์–ธ์–ด๋ฅผ ๋„ฃ์–ด์ค˜ ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น ์–ธ์–ด๊ฐ€ ์—†๋‹ค๋ฉด 0์ ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

 

์ด๋Ÿฐ์‹์œผ๋กœ ๋ชจ๋“  ์–ธ์–ด๋ฅผ ์ˆœํšŒํ•ด ํ•ด๋‹น ์„ ํ˜ธ๋„์™€ ์–ธ์–ด์ ์ˆ˜๋ฅผ ๊ณฑํ•ด์ค˜ ํ•ฉ์„ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค.

 

func setJobLangaugeScoreAndPreferenceSum(languages:[String],preference:[Int],jobs:[String:[String:Int]]) -> [String:Int] {
    var scores:[String:Int] = [:]
    for (i,lang) in languages.enumerated() {
        for key in jobs.keys {
            let jobScore = jobs[key]?[lang] == nil ? 0 : jobs[key]![lang]!
            if scores[key] == nil {
                scores[key] = preference[i] * jobScore
            }else {
                scores[key]! += preference[i] * jobScore
            }
        }
    }
    return scores
}

 

3. ๊ฐ€์žฅ ์ ์ˆ˜๊ฐ€ ๋†’์€ ์ง์—…๊ตฐ์„ ๊ตฌํ•œ๋‹ค.

 

2์—์„œ ๊ตฌํ•œ ์ดํ•ฉ๋“ค ์ค‘ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ์ฐพ์•„ answer์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ•˜๋‚˜๋งŒ ๊ตฌํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌํ•œ ๋’ค ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค.

 

func findMaxJob(scores:[String:Int]) -> String {
    var answer:[String] = []
    let max = scores.values.max()
    for score in scores {
        if score.value == max {
            answer.append(score.key)
        }
    }
    return answer.sorted().first!
}

 

4. ๊ฐ€์žฅ ์ ์ˆ˜๊ฐ€ ๋†’์€ ์ง์—…๊ตฐ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

func solution(_ table:[String], _ languages:[String], _ preference:[Int]) -> String {
    let jobs:[String:[String:Int]] = setJobLanguageScore(table: table)
    let scores:[String:Int] = setJobLangaugeScoreAndPreferenceSum(languages: languages, preference: preference, jobs: jobs)
    return findMaxJob(scores: scores)
}

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€