프로그래밍/코딩테스트 문제풀이
[Python] 프로그래머스. 2018 카카오 recruit - 캐시 (Level 2)
inspirit941
2020. 3. 24. 19:34
반응형
https://programmers.co.kr/learn/courses/30/lessons/17680
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr


문제 조건대로 구현하는 문제.
This file contains hidden or 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
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 |
반응형