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

[Swift] 2022 KAKAO TECH INTERNSHIP ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ

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

Problem

 

ํ•œ ๋™์•ˆ ๋ฆฟ์ฝ”๋“œ๋งŒ ํ’€๋‹ค๊ฐ€ ์ •๋ง ์˜ค๋žœ๋งŒ์— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋ฅผ ๋“ค์–ด๊ฐ”๋”๋‹ˆ ์ƒˆ๋กœ์šด ์นด์นด์˜ค ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์„œ ํ•œ๋ฒˆ ํ’€์–ด๋ดค๋‹ค.

 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


Solution

 

1. ์ฃผ์–ด์ง„ ์„ฑ๊ฒฉ ์•ŒํŒŒ๋ฒณ์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

    var charDic: [Character:Int] = ["R":0,"T":0,"C":0,"F":0,"J":0,"M":0,"A":0,"N":0]

 

2. survey๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ choice์˜ ๊ฐ’์— ๋งž๊ฒŒ ์ฒซ ๋ฒˆ์งธ ๋˜๋Š” ๋‘ ๋ฒˆ์งธ ์บ๋ฆญํ„ฐ์— ๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.

 

๋งŒ์•ฝ choices์˜ i๋ฒˆ์งธ ๊ฐ’์ด 4๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์— 4์—์„œ ํ•ด๋‹น ๊ฐ’์„ ๋บ€ ์ ˆ๋Œ“๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.

 

4๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๊ธ€์ž์— 4์—์„œ ํ•ด๋‹น ๊ฐ’์„ ๋บ€ ์ ˆ๋Œ“๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.

 

    for i in 0..<survey.count {
        let chars: [Character] = Array(survey[i])

        if choices[i] < 4 {
            charDic[chars[0]]! += abs(4 - choices[i])
        } else {
            charDic[chars[1]]! += abs(4 - choices[i])
        }
    }

 

3. ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ’์— ๋งž๊ฒŒ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๋งŒ๋“ค์–ด์ค˜ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

    let RT = charDic["R"]! >= charDic["T"]! ? "R" : "T"
    let CF = charDic["C"]! >= charDic["F"]! ? "C" : "F"
    let JM = charDic["J"]! >= charDic["M"]! ? "J" : "M"
    let AN = charDic["A"]! >= charDic["N"]! ? "A" : "N"
    
    return RT + CF + JM + AN

Result

 


Source Code

 

func solution(_ survey:[String], _ choices:[Int]) -> String {
    var charDic: [Character:Int] = ["R":0,"T":0,"C":0,"F":0,"J":0,"M":0,"A":0,"N":0]
    
    for i in 0..<survey.count {
        let chars: [Character] = Array(survey[i])

        if choices[i] < 4 {
            charDic[chars[0]]! += abs(4 - choices[i])
        } else {
            charDic[chars[1]]! += abs(4 - choices[i])
        }
    }
    
    let RT = charDic["R"]! >= charDic["T"]! ? "R" : "T"
    let CF = charDic["C"]! >= charDic["F"]! ? "C" : "F"
    let JM = charDic["J"]! >= charDic["M"]! ? "J" : "M"
    let AN = charDic["A"]! >= charDic["N"]! ? "A" : "N"
    
    return RT + CF + JM + AN
}

P.S

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” 1๋‹จ๊ณ„ ์˜€๋Š”๋ฐ ์ข€ ํ—ค๋งจ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค.

 

1~7๊นŒ์ง€ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฏ€๋กœ ๋‚œ 1~3์ด๋ผ๋ฉด 8์—์„œ ๋บ€ ๊ฐ’์„ ๋”ํ•˜๊ณ  5~7์ด๋ฉด ๊ทธ๋ƒฅ ๋”ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค.

 

๊ทผ๋ฐ 65์ ์œผ๋กœ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

 

๋‚ด ์ƒ๊ฐ์—” 4 - 1 = 3, 4 - 2 = 2, 4 - 3 = 1, 4 - 4 = 0,4 - 5 = 1, 4 - 6 = 2, 4 - 7 = 3 => 1,2,3,1,2,3

 

8 - 1 = 7, 8 - 2 = 6, 8 - 3 = 5, 5, 6 , 7 => 5,6,7,5,6,7 ์ด๋ฉด ๋˜‘๊ฐ™์ด ์ •๋‹ต์ด ๋‚˜์™€์•ผ ํ•˜์ง€ ์•Š๋‚˜?.. 

 

์ดํ•ด๊ฐ€ ์•ˆ๋œ๋‹ค...

 

ํ˜น์‹œ ์™œ ํ‹€๋ ธ๋Š”์ง€ ์•„์‹œ๋Š” ๋ถ„์€ ๋Œ“๊ธ€๋กœ ์ข€ ์•Œ๋ ค์ฃผ์‹ญ์‡ผ..ใ…œ

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€