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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - [1์ฐจ] ์บ์‹œ (Lv.2)

๊ฐ์ž ๐Ÿฅ” 2022. 9. 10. 02:25
๋ฐ˜์‘ํ˜•

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

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

 

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

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

programmers.co.kr

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์šฐ์„  LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผํ–ˆ๋‹ค. ์ง„์งœ ๋งˆ์นจ,, ๋งˆ์นจ,, ์˜ค๋Š˜ ์•„์นจ์— CS ์Šคํ„ฐ๋””๋ฅผ ํ–ˆ๋Š”๋ฐ ๊ฑฐ๊ธฐ์„œ LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋‚˜์™”๋‹ค. ๊ทผ๋ฐ ์ž์„ธํ•œ ์„ค๋ช…์€... ์•„์ง ์ดํ•ดํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ„ ์ƒํƒœ๋‹ค.. ๊ทผ๋ฐ ๋ฐ”๋กœ๋‚˜์™€ ๋ฒ„๋ฆฌ๋‹ค๋‹ˆ!!

LRU๋Š” ํŽ˜์ด์ง• ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋“ฑ์žฅํ•œ ๊ฐœ๋…์ด์—ˆ๋‹ค. 
๊ฐœ๋…์€ Least Recently Used๋กœ, ๊ฐ€์žฅ ์˜ค๋žซ๋™์•ˆ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ํŽ˜์ด์ง€๋ฅผ ๊ต์ฒดํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ํ๋กœ ๊ตฌํ˜„๋˜๋Š” ๋ฐฉ์‹์ด๊ณ , ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ์—์„œ ์ง€์šฐ๊ณ  ๋งจ ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. ๋งŒ์•ฝ ํ”„๋ ˆ์ž„์˜ ๊ธธ์ด๊ฐ€ ๋ชจ์ž๋ž„ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•œ๋‹ค. 

์ด๊ฒŒ ํŽ˜์ด์ง• ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, Cache ์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์บ์‹œ์—์„œ ์‚ฌ์šฉ ๊ณต๊ฐ„์ด ๋ถ€์กฑํ•  ๋•Œ, ๊ฐ€์žฅ ์˜ค๋žซ๋™์•ˆ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๊ณ , ์ƒˆ๋กœ์šด ๋…€์„์„ ๋ฐฐ์น˜ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๋™์ž‘๋œ๋‹ค. 

๊ทธ๋Ÿผ, ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š๋ƒ? LRU๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์€ ์œ„์— ์ฃผํ™ฉ ๊ธ€์”จ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•ด๋‘์—ˆ๋‹ค. ๊ทผ๋ฐ ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๊ฐ€๋‹ˆ, ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ ค๋ณด์ž.

๋งŒ์•ฝ Cache Size ๊ฐ€ 3์ด๊ณ , [1, 2, 1, 3, 4, 1, 2, 4]๊ฐ€ ์ฐธ์กฐ๊ฐ’์ด๋ผ๊ณ  ์น˜์ž. ๊ทธ๋Ÿผ LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค.

๋ณด๋ฉด, Caeche Hit ์ด๋ฉด, ๊ธฐ์กด์— ์žˆ๋˜ ํ•ด๋‹น ๊ฐ’์„ ์‚ญ์ œํ•˜๊ณ , ๋งจ ์•ž Head์— ๊ทธ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. ๋งŒ์•ฝ, Miss ์ด๋ฉด, ๊ทธ๋ƒฅ ๊ฐ€์žฅ ์ตœ๊ทผ๊ฐ’์„ Head ์— ๋„ฃ๊ณ , ๋งŒ์•ฝ ๋ฉ”๋ชจ๋ฆฌ์˜ size๊ฐ€ 3์ด ๋„˜์–ด๊ฐ€๋ฉด, ๊ฐ€์žฅ ์ฒ˜์Œ์— ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋ฉด์„œ ๊ธธ์ด 3์„ ์œ ์ง€ํ•œ๋‹ค.

 

์‚ฌ์‹ค ๊ทธ๋ฆผ์ฒ˜๋Ÿผ, Caeche ๋Š” ์‹ค์ œ๋กœ Linked List (head์™€ tail์ด ์žˆ๋Š” ๊ตฌ์กฐ)๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋„ ์ด ๋ฌธ์ œ๋ฅผ Linked List๋กœ ํ’€์–ด์•ผํ• ๊นŒ? ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด ์งˆ๊ฒƒ ๊ฐ™์•„์„œ, ๊ทธ๋ƒฅ ๋จธ๋ฆฌ์— ์„ธ์šด ๋กœ์ง์„ ๊ทธ๋Œ€๋กœ for๋ฌธ๊ณผ if๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

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

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EC%BA%90%EC%8B%9C/main.swift

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

func solution(_ cacheSize:Int, _ cities:[String]) -> Int {

    let newCitiies = cities.map {
        return $0.lowercased()
    }

    var cacheArray = [String]()
    var totalTime = 0

    for city in newCitiies {
        // cache hit
        if cacheArray.contains(city) {
            totalTime += 1
            cacheArray.remove(at: cacheArray.firstIndex(of: city)!)
            cacheArray.append(city)
        } else { // cache miss
            totalTime += 5
            cacheArray.append(city)
            if cacheArray.count > cacheSize {
                cacheArray.removeFirst()
            }
        }
    }
    return totalTime
}

 

๋ฐ˜์‘ํ˜•