Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 혼공머신
- 캐글
- 브라이틱스 서포터즈
- 포스코 청년
- 팀 분석
- Brightics를 이용한 분석
- 영상제작기
- Brightics
- 모델링
- 데이터분석
- Brightics Studio
- 삼성SDS
- 혼공머신러닝딥러닝
- 직원 이직률
- 삼성 SDS Brigthics
- 혼공학습단
- 데이터 분석
- Brigthics
- 삼성SDS Brigthics
- 삼성SDS Brightics
- Brigthics Studio
- 개인 의료비 예측
- 삼성 SDS
- 추천시스템
- 직원 이직여부
- 노코드AI
- Brigthics를 이용한 분석
- 브라이틱스
- 포스코 아카데미
- 혼공
Archives
- Today
- Total
데이터사이언스 기록기📚
[프로그래머스] Level 2_[1차]캐시(2018 KAKAO BLIND RECRUITMENT) 본문
Coding Test/프로그래머스(Python)
[프로그래머스] Level 2_[1차]캐시(2018 KAKAO BLIND RECRUITMENT)
syunze 2022. 10. 13. 12:36📌문제 유형
2018 KAKAO BLIND RECRUITMENT
📌문제
- LRU 알고리즘이란?
📌나의 문제풀이
- cities : city는 대소문자 구분하지 않아 lower로 통일
- cachesize == 0일때는 크기만큼 리턴
- cacheSize보다 작을 때, 같을 때 고려하기
def solution(cacheSize, cities):
answer = 0
cities = [city.lower() for city in cities]
visit = []
if cacheSize == 0:
return 5 * len(cities)
for city in cities:
# cacheSize보다 작을 때
if len(visit) < cacheSize:
if city not in visit:
visit.append(city)
answer += 5
else:
visit.remove(city) # visit에 있으면 삭제 후
visit.append(city) # 뒤에 다시 넣기
answer += 1
# cacheSize랑 같을 때
else:
if city not in visit:
visit.append(city)
answer += 5
visit.pop(0) # cacheSize 맞추기위해 삭제
else:
visit.remove(city)
visit.append(city)
answer += 1
return answer
📌다른 사람들의 문제풀이
1) deque의 maxlen 사용
- maxlen
import collections
def solution(cacheSize, cities):
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
📌리뷰
- deque의 maxlen으로 리스트 크기 조절 가능
- LRU 알고리즘
728x90
'Coding Test > 프로그래머스(Python)' 카테고리의 다른 글
[프로그래머스] Level 2_괄호 회전하기(월간 코드 챌린지 시즌2) (0) | 2022.10.14 |
---|---|
[프로그래머스] Level 2_행렬의 곱셈(연습문제) (0) | 2022.10.13 |
[프로그래머스] Level 2_점프와 순간 이동(Summer/Winter Coding(~2018)) (0) | 2022.10.13 |
[프로그래머스] Level 2_숫자의 표현(연습문제) (0) | 2022.10.12 |
[프로그래머스] Level 2_멀리뛰기(연습문제) (1) | 2022.10.08 |
Comments