Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) kakao - ์‹คํŒจ์œจ (2019 KAKAO BLIND RECRUITMENT)

๊ฐ์ž ๐Ÿฅ” 2022. 8. 7. 01:41
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/42889\

 

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

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

programmers.co.kr

 

๐ŸŸ  ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EC%8B%A4%ED%8C%A8%EC%9C%A8/filter%EC%82%AC%EC%9A%A9_%EC%8B%9C%EA%B0%84%EC%B4%88%EA%B3%BC.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

๊ฐ„๋‹จํ•˜๊ฒŒ, ๋ฐฐ์—ด์—์„œ ์ˆ˜๋ฅผ ์„ธ์–ด์„œ ๋‚˜๋ˆ ์ฃผ๋Š” ๋ฌธ์ œ์ด๋‹ค. stage๋ณ„๋กœ failure๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ผ์„œ, ๋ฐฐ์—ด์„ ๋‘๊ฐœ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ+ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ์–ด์ฐจํ”ผ key/value ๋ณ„๋กœ ์ •๋ ฌํ•ด์ฃผ๋ฉด ๋˜๋‹ˆ๊นŒ!

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น stage๋ฅผ ๋‹ฌ์„ฑํ•œ์‚ฌ๋žŒ, ๋‹ฌ์„ฑํ–ˆ์ง€๋งŒ ๋ชปํ•ด๊ฒฐํ•œ์‚ฌ๋žŒ์„ ์„ธ์–ด์ฃผ๊ธฐ์œ„ํ•ด filter+count๋ฅผ ์ด์šฉํ•ด์„œ ์„ธ์–ด์ฃผ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ test case 5, 9, 22๋ฒˆ์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค ใ… ใ…  

import Foundation

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    var failure = [Int: Double]()

    for i in 1..<N+1 {
        let failNum = stages.filter({ $0 == i }).count
        let clearNum = stages.filter({ $0 >= i }).count
        failure[i] = Double(failNum) / Double(clearNum)
    }
    let sortArr = failure.sorted(by: <).sorted(by: { $0.1 > $1.1 })
    let result = sortArr.map{ $0.key }
    
    return result
}

 

์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด์„œ 5, 9, 22 ๋ฒˆ์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์•˜๋‹ค. ๋ฌธ์ œ๋Š”, filter ๊ณ ์ฐจํ•จ์ˆ˜๊ฐ€ for ๋ฌธ ์•ˆ์— ๋“ค์–ด๊ฐ€๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋„ˆ๋ฌด ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์—, filter๋ฅผ ๋นผ๊ณ  countํ•ด๋ณด๋ผ๋Š” ์กฐ์–ธ์ด ์žˆ์—ˆ๋‹ค.

 

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EC%8B%A4%ED%8C%A8%EC%9C%A8/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

๊ทธ๋ž˜์„œ ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ, filter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ count ํ•ด์ฃผ์—ˆ๋‹ค. index๋ฒˆํ˜ธ๋ฅผ ๊ทธ๋Œ€๋กœ stage ๋ฒˆํ˜ธ๋กœ ๊ฐ€์ ธ๊ฐ€๊ณ , ๋“ฑ์žฅํ•˜๋ฉด index์— ๋งž์ถฐ์„œ +1 ์”ฉ ํ•ด์ฃผ๋ฉด์„œ count ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋‹ˆ, ํ†ต๊ณผ์˜€๋‹ค!

import Foundation

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    var failure = [Int: Double]()
    var total = Double(stages.count) // 8.0
    var countArr = Array(repeating: 0, count: N+2) // [0, 0, 0, 0, 0, 0]

    for num in stages {
        countArr[num] += 1 // [0, 1, 3, 2, 1, 0, 1]
    }

    for i in 1..<N+1 {
        if countArr[i] == 0 {
            failure[i] = 0.0
        } else {
            total = total - Double(countArr[i])
            failure[i] = Double(countArr[i]) / total
        }
    }
    let sortArr = failure.sorted(by: <).sorted(by: { $0.1 > $1.1 })
    let result = sortArr.map{ $0.key }

    return result
}

 

์• ์ดˆ์— ๊ฐ ์Šคํ…Œ์ด์ง€ ๋ณ„๋กœ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๊ณ  ๋‚œ๋’ค for๋ฌธ์„ ๋Œ๋ ค์ฃผ๋ฉด, ์„ ํ˜•์ ์ธ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, for๋ฌธ์•ˆ์— filter๋ฅผ ๋„ฃ๊ฒŒ ๋˜๋ฉด, O(n^2) ๊นŒ์ง€ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์—, ํŠน์ • ์ผ€์ด์Šค๊ฐ€ ํฐ ๊ฒƒ๋“ค์€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.

 


๐ŸŸ  ๊ณ ์ฐฐ

์• ํ”Œ ์•„์นด๋ฐ๋ฏธ์—์„œ ์ข‹์€ ๊ธฐํšŒ๋กœ ์• ํ”Œ ๊ด€๊ณ„์ž๋ถ„๊ป˜ ์งˆ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๊ฐ€ ์žˆ์—ˆ๊ณ , ์ด๋•Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ด€๋ จ ์งˆ๋ฌธํ•œ ์ ์ด ์žˆ์—ˆ๋‹ค. '๊ณ ์ฐจํ•จ์ˆ˜'์— ๋Œ€ํ•œ ๋Œ€ํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ์—ˆ๊ณ , ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€ ๋•Œ ์™œ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง€๊ณ , ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์ž์ฃผ๋‚˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์—ฌ์ญค๋ณด์•˜๋‹ค.

๊ทธ ๋ถ„๊ป˜์„œ๋Š” ์ ˆ๋Œ€ swift์—์„œ ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง€๊ฒŒ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋†“์ง€ ์•Š์•˜๋‹ค๋ฉฐ, ๋‚ด๊ฐ€ ์ง  ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๊ณ  ํ•˜์…จ๋‹ค. ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ map, filter, reduce ๊ฐ ๊ณ ์ฐจํ•จ์ˆ˜๋Š” O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค. ๋ณ„๋กœ ๊ทธ๋‹ฅ ํฐ ๊ฑด ์•„๋‹Œ๊ฑฐ๊ฐ™์ง€๋งŒ, ์ด๊ฒŒ for ๋ฌธ ์•ˆ์— ๋“ค์–ด์žˆ์œผ๋ฉด, ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ๋Š˜์–ด๋‚˜๊ธฐ๋•Œ๋ฌธ์— ๋ฐฉ๊ธˆ๊ณผ ๊ฐ™์€, ์‹œ๊ฐ„ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๋ฌธ์ œ๋ฅผ ๊ฒช์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. 

์•„ํ•˜! ๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” ๋Œ€๋ถ€๋ถ„ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์“ฐ์ง€๋ง๋ผ๋Š”๊ฑฐ๊ตฌ๋‚˜!! ์™œ๋ƒ๋ฉด ๋Œ€๋ถ€๋ถ„ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋‹ˆ๊นŒ..! ๋‚˜๋„ ์•ž์œผ๋กœ๋Š” ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ• ๋•Œ ์™ธ์—๋Š” ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์กฐ์‹ฌํ•ด์•ผ๊ฒ ๋”ฐ!

 

 

๋ฐ˜์‘ํ˜•