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

2018 KAKAO BLIND RECRUITMENT [1์ฐจ] ์บ์‹œ Swift

by Fomagran ๐Ÿ’ป 2020. 10. 17.
728x90
๋ฐ˜์‘ํ˜•
 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ์บ์‹œ

3 [Jeju, Pangyo, Seoul, NewYork, LA, Jeju, Pangyo, Seoul, NewYork, LA] 50 3 [Jeju, Pangyo, Seoul, Jeju, Pangyo, Seoul, Jeju, Pangyo, Seoul] 21 2 [Jeju, Pangyo, Seoul, NewYork, LA, SanFrancisco, Seoul, Rome, Paris, Jeju, NewYork, Rome] 60 5 [Jeju, Pangyo, S

programmers.co.kr

Foma's ํ’€์ด

๋ฌธ์ œํ’€์ด

๋จผ์ € ์ฒซ๋ฒˆ์งธ๋กœ LRU๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ด์•ผํ• ํ…๋ฐ์š”. 

 

LRU๋ž€? -> Least Recently Used ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ๊ต์ฒดํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

์ฆ‰, ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ๊ฒƒ์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

์ด ์›๋ฆฌ๋ฅผ ์ ์šฉํ•˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ’์ด ๋“ค์–ด์™”์„๋•Œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ์ œ๊ฑฐ๋˜๊ณ  ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— 

 

์ƒˆ๋กœ์šด ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ๋กœ ์•Œ์•„๋ณด๋ฉด 3์ข…๋ฅ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋„์‹œ๋“ค์— (1)์ œ์ฃผ,(2)ํŒ๊ต,(3)๋‰ด์š•์ด ๋‹ด๊ฒจ์žˆ๊ณ  ์„œ์šธ์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋„์‹œ๋ฅผ ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด

 

๋งจ ๋งˆ์ง€๋ง‰์— ๋‰ด์š•์ด ๋‚˜๊ฐ€๊ณ  ๋งจ ์ฒซ๋ฒˆ์งธ์— ์„œ์šธ์ด ์ถ”๊ฐ€๋˜์–ด (1)์„œ์šธ,(2)์ œ์ฃผ,(3)ํŒ๊ต๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  cacheSize๋Š” ๋„์‹œ์˜ ์ข…๋ฅ˜๋ฅผ ์–ผ๋งŒํผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š”๊ฐ€์ž…๋‹ˆ๋‹ค.

 

์ฆ‰, cacheSize๊ฐ€ 3์ด๋ผ๋ฉด 3๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋„์‹œ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ๋„์‹œ๊ฐ€ ๋“ค์–ด์˜ฌ ๋•Œ ๋งŒ์•ฝ ์ด๋ฏธ ๊ทธ ๋„์‹œ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

 

์ด ๊ฒฝ์šฐ์—๋Š” ๋„์‹œ๋“ค์—์„œ ์›๋ž˜ ๊ทธ ๋„์‹œ๊ฐ€ ์žˆ๋˜ ์ˆœ์„œ๊ฐ€ ์‚ญ์ œ๋˜๊ณ  ๋งจ ์ฒซ๋ฒˆ์งธ์— ๋„์‹œ๊ฐ€ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.

 

์œ„์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ๋„์‹œ๋“ค์— (1)์ œ์ฃผ,)(2)ํŒ๊ต,(3)๋‰ด์š•์ด ๋‹ด๊ฒจ์žˆ๊ณ  ํŒ๊ต๋ผ๋Š” ์›๋ž˜ ์žˆ๋˜ ๋„์‹œ๋ฅผ ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด

 

์›๋ž˜ ์žˆ๋˜ ๋„์‹œ๋“ค์—์„œ 2๋ฒˆ์งธ์ธ ํŒ๊ต๊ฐ€ ์‚ญ์ œ๋˜๊ณ  ๋งจ ์ฒซ๋ฒˆ์งธ์— ํŒ๊ต๊ฐ€ ์ถ”๊ฐ€๋˜์–ด (1)ํŒ๊ต,)(2)์ œ์ฃผ,(3)๋‰ด์š•์ด ๋ฉ๋‹ˆ๋‹ค.

 

์ด์™€ ๊ฐ™์ด ์›๋ž˜ ์žˆ๋˜ ๋„์‹œ๋ฅผ ๋„ฃ์œผ๋ฉด cache hit์ด๋ฉฐ ์ˆ˜ํ–‰์‹œ๊ฐ„์ด 1์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์ด๊ณ 

 

์ƒˆ๋กœ์šด ๋„์‹œ๋ฅผ ๋„ฃ์œผ๋ฉด cache miss๋กœ ์ˆ˜ํ–‰์‹œ๊ฐ„์ด 5๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œํ’€์ด

์บ์‹œ๋“ค์„ ๋‹ด์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. -> var cache = [String]()

 

์ˆ˜ํ–‰์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. -> var time = 0

 

๋„์‹œ๋“ค(cities)๋ฅผ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. -> for city in cities { ... }

 

๋ฌธ์ œ์—์„œ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์„ ํ•˜์ง€์•Š๋Š”๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ ๋„์‹œ์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. -> let lowerCity = city.lowercased()

 

๋งŒ์•ฝ cacheSize๊ฐ€ 0์ด๋ผ๋ฉด ๋„์‹œ๋“ค์ด ์ถ”๊ฐ€๋  ๋•Œ ํ•ญ์ƒ cache miss์ด๋ฏ€๋กœ ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ๋„์‹œ๋“ค์˜ ๊ฐฏ์ˆ˜ ๊ณฑํ•˜๊ธฐ 5๋ฅผ ํ•ด์ค€ ๋’ค for๋ฌธ์„ ํƒˆ์ถœํ•ฉ๋‹ˆ๋‹ค. - > if cacheSize == 0 {

            time = 5*cities.count

            break

        }

 

๋งŒ์•ฝ ์บ์‹œ์•ˆ์— ์ด๋ฏธ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์„œ ์‚ญ์ œํ•ด์ค€๋’ค ์ˆ˜ํ–‰์‹œ๊ฐ„์— +1์„ ํ•ด์ค๋‹ˆ๋‹ค. -> if cache.contains(lowerCity) {

            cache.remove(at: cache.firstIndex(of: lowerCity)!)

            time += 1

        }

 

๊ทธ๋ ‡์ง€ ์•Š๊ณ (์ƒˆ๋กœ์šด ๋„์‹œ๋ผ๋ฉด) ์บ์‹œ๊ฐ€ ๊ฝ‰ ์ฐผ์„๋• ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ๊ฐ’์„ ์‚ญ์ œํ•ด์ค€๋’ค ์ˆ˜ํ–‰์‹œ๊ฐ„์— +1์„ ํ•ด์ค๋‹ˆ๋‹ค. ->

else{

            if cache.count == cacheSize{

                cache.removeLast()

            }

            time += 5

        }

 

๊ทธ๋ฆฌ๊ณ  ํฌํ•จ๋˜์žˆ๋“  ์•ˆ๋˜์žˆ๋Š” ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ์— ๋„์‹œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. -> cache.insert(lowerCity, at: 0)

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ->  return time

 

์ „์ฒด์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Foundation
 
func solution(_ cacheSize:Int, _ cities:[String]) -> Int {
    var cache = [String]()
    var time = 0
    for city in cities {
        let lowerCity = city.lowercased()
        if cacheSize == 0 {
            time = 5*cities.count
            break
        }
        if cache.contains(lowerCity) {
            cache.remove(at: cache.firstIndex(of: lowerCity)!)
            time += 1
        }else{
            if cache.count == cacheSize{
                cache.removeLast()
            }
            time += 5
        }
        cache.insert(lowerCity, at: 0)
    }
    return time
}

 

์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ๊ฒƒ

removeLast์™€ popLast์˜ ์ฐจ์ด

 

removeLast๋Š” ๋งŒ์•ฝ ๋ฐฐ์—ด์— ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€๋งŒ popLast๋Š” ์•„๋ฌด๊ฒƒ๋„ ์—†์œผ๋ฉด nil๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๋ฐฐ์—ด์•ˆ์— ํŠน์ •๊ฐ’์˜ ์ธ๋ฑ์Šค ์•Œ์•„๋‚ด๊ธฐ

 

firstIndex(of:value)

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€