코딩테스트 연습 - [1차] 캐시
3 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"] 50 3 ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"] 21 2 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Ro
programmers.co.kr
풀이
collections의 deque를 사용하여 해결했습니다.
코드
from collections import deque
def solution(cacheSize, cities):
answer = 0
cache = deque()
if cacheSize == 0:
return len(cities) * 5
cur = 0
for city in cities:
city = city.upper()
if city in cache:
answer += 1
cache.remove(city)
cache.appendleft(city)
else:
answer += 5
if cur < cacheSize:
cache.appendleft(city)
cur += 1
else:
cache.pop()
cache.appendleft(city)
return answer
pyo7410/Algorithm
1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.
github.com
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[Python] 프로그래머스 후보키 (0) | 2021.04.17 |
---|---|
[Python] 프로그래머스 오픈채팅방 (0) | 2021.04.11 |
[Python] 수식 최대화 (0) | 2021.03.28 |
[Python] 튜플 (0) | 2021.03.27 |
[Python] 순위 검색 (0) | 2021.03.21 |