728x90
나의 풀이
from collections import deque
def solution(cacheSize, cities):
if not cacheSize:
return 5 * len(cities)
cache = deque()
answer = 0
for c in cities:
c = c.lower()
if c in cache:
cache.remove(c)
answer += 1
else:
if len(cache) == cacheSize:
cache.popleft()
answer += 5
cache.append(c)
return answer
deque의 maxlen을 활용한 풀이
def solution(cacheSize, cities):
import collections
cache = collections.deque(maxlen=cacheSize)
time = 0
for i in cities:
s = i.lower()
if s in cache:
cache.remove(s)
cache.append(s)
time += 1
else:
cache.append(s)
time += 5
return time
반응형
'Coding Test > Programmers' 카테고리의 다른 글
[프로그래머스/파이썬] 점프와 순간 이동 (0) | 2022.06.17 |
---|---|
[프로그래머스/파이썬] 이진 변환 반복하기 (0) | 2022.06.17 |
[프로그래머스/파이썬] 스킬트리 (0) | 2022.06.17 |
[프로그래머스/파이썬] 모음사전 (0) | 2022.06.17 |
[프로그래머스/파이썬] 전력망을 둘로 나누기 (0) | 2022.06.17 |