You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
해당 city를 cache의 맨 앞에 집어넣고, 캐시 크기보다 크거나 같은 value를 가진 (가장 최근에 사용되지 않은) 항목을 삭제한다.
캐시에 현재 city를 key값으로 가지는 값이 있는 경우 - cache hit
해당 city를 cache의 맨 앞으로 옮긴다.
💬 풀이
import Foundation
func solution(cacheSize:Int, cities:[String])->Int{varcache:[String:Int]=[:]varres=0forcityin cities {
// 도시이름 대소문자 구분 않기 위해 lowercased 처리
letcity= city.lowercased()
// 캐시에 도시이름이 들어있지 않은 경우 - cache miss
ifcache[city]==nil{cache[city]=0forcin cache {
// value(index)가 캐시 크기와 같다면
if c.value == cacheSize {
// 삭제 (가장 최근에 사용되지 않은 항목이므로)
cache.removeValue(forKey: c.key)}else{
// 한칸씩 밀리도록 하기 위해 value에 1 더한다
cache[c.key]! +=1}}
// cache miss이므로 res에 5 더한다
res +=5}else{
// 캐시에 도시이름이 들어있는 경우 - cache hit
// 그 전 인덱스에 있던 도시들의 value를 1씩 더해주고
forcin cache.sorted(by:{ $0.value < $1.value }){if c.value <cache[city]! {cache[c.key]! +=1}}
// 해당 도시이름을 1번으로 옮긴다
cache[city]=1
// cache hit이므로 res에 1 더한다
res +=1}}return res
}
The text was updated successfully, but these errors were encountered:
💬 문제
[1차] 캐시
💬 Idea
cache miss
cache hit
💬 풀이
The text was updated successfully, but these errors were encountered: