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 |
29 | 30 |
Tags
- 파이썬알고리즘풀기
- 파이썬릿코드
- leetcode풀기
- python 릿코드
- 상가수익률계산기
- python sorted
- 파이썬 프로그래머스
- leetcode풀이
- 파이썬 알고리즘 풀기
- python 알고리즘
- 파이썬 릿코드
- binary search
- 릿코드 풀기
- 잇츠디모
- 릿코드
- 릿코드 파이썬
- 파이썬릿코드풀기
- python zip_longest
- 알고리즘풀이
- python Leetcode
- 코틀린기초
- python priority queue
- python xor
- 알고리즘풀기
- 파이썬알고리즘
- LeetCode
- 릿코드풀기
- leetcode 풀기
- 릿코드풀이
- 파이썬 알고리즘
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 146. LRU Cache 본문
반응형
146. LRU Cache
https://leetcode.com/problems/lru-cache/
LRU Cache - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1) dict()
dict() 자료 구조를 이용해서 쉽게 풀 수 있습니다.
참고적으로, python 3.7 이상 부터의 dict는 데이터의 삽입 순서를 보장합니다.
Ordered dictionaries are just like regular dictionaries but have some extra capabilities relating to ordering operations. They have become less important now that the built-in dict class gained the ability to remember insertion order (this new behavior became guaranteed in Python 3.7).
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = {}
def get(self, key):
value = self.cache.get(key, -1)
if value == -1:
return value
del self.cache[key]
self.cache[key] = value
return value
def put(self, key, value):
# 이미 키가 있는 경우
if key in self.cache:
del self.cache[key]
self.cache[key] = value
return
if len(self.cache) >= self.capacity:
del self.cache[next(iter(self.cache))]
self.cache[key] = value
함께 보면 좋은 글:
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 101. Symmetric Tree (0) | 2022.03.26 |
---|---|
LeetCode 풀기 - 328. Odd Even Linked List (0) | 2022.03.26 |
LeetCode 풀기 - 895. Maximum Frequency Stack (0) | 2022.03.19 |
LeetCode 풀기 - 71. Simplify Path (0) | 2022.03.15 |
LeetCode 풀기 - 2. Add Two Numbers (0) | 2022.03.11 |
Comments