Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created March 24, 2020 10:33
Show Gist options
  • Save inspirit941/fab12819790b6e8847ee53b40573dd04 to your computer and use it in GitHub Desktop.
Save inspirit941/fab12819790b6e8847ee53b40573dd04 to your computer and use it in GitHub Desktop.
def solution(cacheSize, cities):
# 캐시 사이즈가 0이면, 모든 input이 전부 cache miss다.
if cacheSize == 0:
return len(cities) * 5
cache = []
runtime = 0
for city in cities:
city = city.lower()
# cache에 존재하지 않는 경우
if city not in cache:
# cache miss
runtime += 5
# 캐시 등록
cache.append(city)
# 캐시 크기가 초과된 경우, 가장 오랜 시간 쓰이지 않은 캐시값을 제거한다.
if len(cache) > cacheSize:
cache.pop(0)
else:
runtime += 1
# 이미 있는 캐시값을 다시 가져올 경우,
# 해당 위치의 캐시값을 삭제하고, 캐시 맨 뒤에 배치한다.
cache.pop(cache.index(city))
cache.append(city)
return runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment