-
[프로그래머스] [1차] 캐시Engineering WIKI/Programmers 2022. 5. 30. 16:45
코딩테스트 연습 - [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
내 풀이
def solution(cacheSize, cities): answer = 0 cache_list = list() for city in cities: city = city.lower() # Miss if city not in cache_list: if len(cache_list) < cacheSize: cache_list.append(city) elif cacheSize != 0: cache_list.pop(0) cache_list.append(city) answer += 5 else: cache_list.pop(cache_list.index(city)) cache_list.append(city) answer += 1 return answer
다른사람 풀이
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
'Engineering WIKI > Programmers' 카테고리의 다른 글
[프로그래머스] 더 맵게 (1) 2022.06.04 [프로그래머스] 영어 끝말잇기 (0) 2022.06.01 [프로그래머스] 가장 큰 정사각형 (0) 2022.06.01 [프로그래머스] 구명보트 (0) 2022.05.31 [프로그래머스] 땅따먹기 (0) 2022.05.26 [프로그래머스] 다음 큰 숫자 (0) 2022.05.25 [프로그래머스] 올바른 괄호 (0) 2022.05.24 [프로그래머스] 숫자의 표현 (0) 2022.05.24