if문을 사용하여 경우를 세개로 나누어 구현했다.
def solution(cacheSize, cities):
answer = 0
cache = list()
if cacheSize == 0:
return 5 * len(cities)
for i in cities:
i = i.lower()
if i in cache:
cache.remove(i)
cache.insert(0, i)
answer += 1
elif len(cache) < cacheSize:
cache.insert(0, i)
answer += 5
else:
cache.pop()
cache.insert(0, i)
answer += 5
return answer
from collections import deque
def solution(cacheSize, cities):
answer = 0
cache = deque(maxlen=cacheSize)
if not cacheSize:
return 5 * len(cities)
for i in cities:
s = i.lower()
if s in cache:
cache.remove(s)
cache.append(s)
answer += 1
else:
cache.append(s)
answer += 5
return answer