티스토리 뷰
728x90
문제
https://programmers.co.kr/learn/courses/30/lessons/17680
외부에서 데이터를 긁어오는데 너무 오래걸려서 db에 캐시를 적용하여 성능개선을 시도.
조건
- 캐시 교체 알고리즘은
LRU
(Least Recently Used)를 사용한다. cache hit
일 경우 실행시간은1
이다.cache miss
일 경우 실행시간은5
이다.
입력 형식
- 캐시 크기(
cacheSize
)와 도시이름 배열(cities
)을 입력받는다. cacheSize
는 정수이며, 범위는 0 ≦cacheSize
≦ 30 이다.cities
는 도시 이름으로 이뤄진 문자열 배열로, 최대 도시 수는 100,000개이다.- 각 도시 이름은 공백, 숫자, 특수문자 등이 없는 영문자로 구성되며, 대소문자 구분을 하지 않는다. 도시 이름은 최대 20자로 이루어져 있다.
입력된 도시이름 배열을 순서대로 처리할 때 총 실행시간을 return하는 문제
접근방법
LRU 알고리즘이란?
- 가장 최근에 사용되지 않은걸 제거하는 알고리즘. 이 알고리즘의 기본 가설은 가장 오랫동안 사용하지 않았던 데이터는 앞으로도 사용할 확률이 적다는 것.
LRU 예제

대략적으로 이렇게 생각했어요
loop {
if cache.count < size {
if 캐시에 값 없으면 { 넣어줌 }
else { 업데이트 }
} else {
값이 있다면 {
업데이트
} 없으면 {
가장 오래된것에 교체
}
}
}
캐시는 push pop이 순서 상관없이 일어날거라 생각해서, 만약 캐시크기가 커지면 shift 하는 시간이 오래걸릴 것 같단 생각에 딕셔너리를 사용했다. 짜다보니 코드가 지져분해졌어요 ㅠ _ㅜ..
코드
- 딕셔너리 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func solution(_ cacheSize:Int, _ cities:[String]) -> Int { | |
if cacheSize == 0 { return cities.count*5 } | |
var cache: [String:Int] = [:] | |
var cacheForKey: [Int:String] = [:] | |
var idxList: Set<Int> = [] | |
var rst = 0 | |
var idx = 0 | |
let update: (Int, String) -> () = { index, ipt in | |
cache[ipt] = idx | |
cacheForKey[idx] = ipt | |
idxList.remove(index) | |
idxList.insert(idx) | |
idx += 1 | |
rst += 1 | |
} | |
let insert: (String) -> () = { ipt in | |
cache[ipt] = idx | |
cacheForKey[idx] = ipt | |
idxList.insert(idx) | |
idx += 1 | |
rst += 5 | |
} | |
let newData: (Int, String, String) -> () = { min, key, ipt in | |
cache.removeValue(forKey: key) | |
cache[ipt] = idx // 갱신 | |
cacheForKey.removeValue(forKey: min) | |
cacheForKey[idx] = ipt | |
idxList.remove(min) | |
idxList.insert(idx) | |
idx += 1 | |
rst += 5 | |
} | |
cities.forEach { | |
let ipt = $0.lowercased() | |
if cache.count < cacheSize { | |
if let index = cache[ipt] { | |
update(index, ipt) | |
} else { | |
insert(ipt) | |
} | |
} else { | |
// 값이 있다면 | |
if let index = cache[ipt] { | |
update(index, ipt) | |
} else { | |
// 가장 오래된 것 삭제 | |
if let min = idxList.min(), let key = cacheForKey[min] { | |
newData(min, key, ipt) | |
} | |
} | |
} | |
} | |
return rst | |
} |
- 깔끔해보이는 풀이(배열)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 깔끔해보이는 코드.. 속도는 딕셔너리가 더 빨랐다. | |
func solution(_ cacheSize:Int, _ cities:[String]) -> Int { | |
if cacheSize == 0 { return cities.count*5 } | |
var rst = 0 | |
var cache: [String] = [] | |
let LRU: (String) -> () = { ipt in | |
if cache.contains(ipt) { cache.remove(at: cache.firstIndex(of: ipt)!)} | |
if cache.count == cacheSize { cache.removeLast() } | |
cache.insert(ipt, at: 0) | |
} | |
cities.forEach { | |
let ipt = $0.lowercased() | |
if cache.contains(ipt) { rst += 1} | |
else { rst += 5} | |
LRU(ipt) | |
} | |
return rst | |
} |
728x90
'개발 블로그 > 알고리즘' 카테고리의 다른 글
[Swift] 프로그래머스 - 후보키 (0) | 2020.11.01 |
---|---|
[Swift] 프로그래머스 - 오픈채팅방 (0) | 2020.10.30 |
[Swift] 프로그래머스 - 프렌즈4블록 (0) | 2020.10.29 |
[Swift] 프로그래머스 - [1차] 뉴스 클러스터링 (0) | 2020.10.24 |
[Swift] 프로그래머스 - 예상 대진표 (0) | 2020.10.23 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 카카오 블라인드 2018
- VIPER 패턴
- UIModalPresentationStyle
- BaseViewController
- 프로그래머스 추석트래픽
- 위젯
- 아키택처
- RxSwift
- presentStyle
- Stack
- 프로그래머스 캐시
- ReactorKit
- 괄호연산
- 자기PR
- 카카오 블라인드2018
- TransitionStyle
- 카카오블라인드2018
- 1차 뉴스 클러스터링
- ios
- Swift
- 백준 1946
- RxDataSource
- 프로그래머스 오픈채팅방
- today extension
- Github Search
- Level 3
- 백준 신입사원
- Widget
- 알고리즘
- BaseTableViewController
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함