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

[Swift] 2022 KAKAO BLIND RECRUITMENT ์–‘๊ถ๋Œ€ํšŒ

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

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–‘๊ถ๋Œ€ํšŒ

๋ฌธ์ œ ์„ค๋ช… ์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ๊ฐ€ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค. ๋ผ์ด์–ธ์€ ์ €๋ฒˆ ์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ ์šฐ์Šน์ž์ด๊ณ  ์ด๋ฒˆ ๋Œ€ํšŒ์—๋„ ๊ฒฐ์Šน์ „๊นŒ์ง€ ์˜ฌ๋ผ์™”์Šต๋‹ˆ๋‹ค. ๊ฒฐ์Šน์ „ ์ƒ๋Œ€๋Š” ์–ดํ”ผ์น˜์ž…๋‹ˆ๋‹ค. ์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ ์šด์˜์œ„์›

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” DFS ๋ฐ ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

1. ๊ฐ ์ ์ˆ˜๋งˆ๋‹ค ์ด๊ธธ์ง€ ์งˆ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

10์ ๋ถ€ํ„ฐ 1์ ๊นŒ์ง€ ์–ดํ”ผ์น˜๋ฅผ ์ด๊ธธ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์ด๊ธธ ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ ์ˆ˜๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ทธ๋ƒฅ 0๋ฐœ์„ ์ด์„œ ๋‹ค๋ฅธ ์ ์ˆ˜๋ฅผ ์–ป์„ ๋ฐฉ๋ฒ•์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

 

DFS๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ์™€ ๊ทธ๋ƒฅ 0๋ฐœ์„ ์˜๊ณ  ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ 2๊ฐ€์ง€๋ฅผ ์žฌ๊ท€ํ•ด์ค๋‹ˆ๋‹ค.

 

func dfs(leftArrow:Int,depth:Int,history:[Int],info:[Int]) {
    var newHistory = history
    var newLeftArrow = leftArrow
    
    ...
    
    if leftArrow > info[10-depth] {
        newLeftArrow -= info[10-depth]+1
        newHistory[depth] = info[10-depth]+1
        dfs(leftArrow: newLeftArrow, depth: depth-1, history: newHistory, info: info)
        newHistory[depth] = 0
    }
    
    dfs(leftArrow: leftArrow, depth: depth-1, history: newHistory, info: info)
}

 

 

2. ๋งŒ์•ฝ ํ™”์‚ด์ด ๋‚จ์ง€ ์•Š์•˜๊ฑฐ๋‚˜, 0์ ๊นŒ์ง€ ์˜จ ๊ฒฝ์šฐ ์ตœ๋Œ€ ์ ์ˆ˜์™€ ๋น„๊ตํ•ด์ค๋‹ˆ๋‹ค.

 

func dfs(leftArrow:Int,depth:Int,history:[Int],info:[Int]) {
    ...
    
    if depth == 0 || leftArrow <= 0 {
        newHistory[0] = depth == 0 ? leftArrow : newHistory[0]
        let score = getScoreGap(history,info)
        if maxScore == score {
            maxHistory.append(newHistory)
        }else if maxScore < score {
            maxScore = score
            maxHistory = [newHistory]
        }
        return
    }
    ...
}

 

* ์–ดํ”ผ์น˜์™€ ๋ผ์ด์–ธ์˜ ์ ์ˆ˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

 

func getScoreGap(_ history:[Int],_ info:[Int]) -> Int {
    var ryan = 0
    var apeach = 0
    for (i,s) in history.enumerated() {
           if s == 0 {
               if info[10-i] != 0 {
                   apeach += i
               }
           }else {
               ryan += i
           }
       }
    return ryan - apeach
}

 

3. ๋งŒ์•ฝ ์ตœ๋Œ€ ์ ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ์•„๋ž˜๋ผ๋ฉด [-1]์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

  if maxScore <= 0 {
        return [-1]
    }

 

4. ์ตœ๋Œ€ ์ ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋‚ฎ์€ ์ ์ˆ˜๊ฐ€ ๋งŽ์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ฒซ ๋ฒˆ์งธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

func sortByMoreLowerScore() -> [Int]{
    var answer:[Int] = maxHistory.first!
    var max = 0
    for history in maxHistory {
        let numCount = history.filter{$0 != 0 }.count
        if max < numCount {
            answer = history
            max = numCount
        }
    }
    return answer.reversed()
}

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€