๋ฌธ์ ์ค๋ช
์ง๋๊ฐ๋ฐํ์์ ๊ทผ๋ฌดํ๋ ์ ์ด์ง๋ ์ง๋์์ ๋์ ์ด๋ฆ์ ๊ฒ์ํ๋ฉด ํด๋น ๋์์ ๊ด๋ จ๋ ๋ง์ง ๊ฒ์๋ฌผ๋ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฝ์ด ๋ณด์ฌ์ฃผ๋ ์๋น์ค๋ฅผ ๊ฐ๋ฐํ๊ณ ์๋ค.
์ด ํ๋ก๊ทธ๋จ์ ํ
์คํ
์
๋ฌด๋ฅผ ๋ด๋นํ๊ณ ์๋ ์ดํผ์น๋ ์๋น์ค๋ฅผ ์คํํ๊ธฐ ์ ๊ฐ ๋ก์ง์ ๋ํ ์ฑ๋ฅ ์ธก์ ์ ์ํํ์๋๋ฐ, ์ ์ด์ง๊ฐ ์์ฑํ ๋ถ๋ถ ์ค ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฒ์๋ฌผ์ ๊ฐ์ ธ์ค๋ ๋ถ๋ถ์ ์คํ์๊ฐ์ด ๋๋ฌด ์ค๋ ๊ฑธ๋ฆฐ๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค.
์ดํผ์น๋ ์ ์ด์ง์๊ฒ ํด๋น ๋ก์ง์ ๊ฐ์ ํ๋ผ๊ณ ๋ฆ๋ฌํ๊ธฐ ์์ํ์๊ณ , ์ ์ด์ง๋ DB ์บ์๋ฅผ ์ ์ฉํ์ฌ ์ฑ๋ฅ ๊ฐ์ ์ ์๋ํ๊ณ ์์ง๋ง ์บ์ ํฌ๊ธฐ๋ฅผ ์ผ๋ง๋ก ํด์ผ ํจ์จ์ ์ธ์ง ๋ชฐ๋ผ ๋๊ฐํ ์ํฉ์ด๋ค.
์ดํผ์น์๊ฒ ์๋ฌ๋ฆฌ๋ ์ ์ด์ง๋ฅผ ๋์, DB ์บ์๋ฅผ ์ ์ฉํ ๋ ์บ์ ํฌ๊ธฐ์ ๋ฐ๋ฅธ ์คํ์๊ฐ ์ธก์ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ํ ์ฌํญ
์ ๋ ฅ ํ์
- ์บ์ ํฌ๊ธฐ(cacheSize)์ ๋์์ด๋ฆ ๋ฐฐ์ด(cities)์ ์ ๋ ฅ๋ฐ๋๋ค.
- cacheSize๋ ์ ์์ด๋ฉฐ, ๋ฒ์๋ 0 โฆ cacheSize โฆ 30 ์ด๋ค.
- cities๋ ๋์ ์ด๋ฆ์ผ๋ก ์ด๋ค์ง ๋ฌธ์์ด ๋ฐฐ์ด๋ก, ์ต๋ ๋์ ์๋ 100,000๊ฐ์ด๋ค.
- ๊ฐ ๋์ ์ด๋ฆ์ ๊ณต๋ฐฑ, ์ซ์, ํน์๋ฌธ์ ๋ฑ์ด ์๋ ์๋ฌธ์๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๋์๋ฌธ์ ๊ตฌ๋ถ์ ํ์ง ์๋๋ค. ๋์ ์ด๋ฆ์ ์ต๋ 20์๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
์ถ๋ ฅ ํ์
- ์ ๋ ฅ๋ ๋์์ด๋ฆ ๋ฐฐ์ด์ ์์๋๋ก ์ฒ๋ฆฌํ ๋, "์ด ์คํ์๊ฐ"์ ์ถ๋ ฅํ๋ค.
์กฐ๊ฑด
- ์บ์ ๊ต์ฒด ์๊ณ ๋ฆฌ์ฆ์ LRU(Least Recently Used)๋ฅผ ์ฌ์ฉํ๋ค.
- cache hit์ผ ๊ฒฝ์ฐ ์คํ์๊ฐ์ 1์ด๋ค.
- cache miss์ผ ๊ฒฝ์ฐ ์คํ์๊ฐ์ 5์ด๋ค.
์ ์ถ๋ ฅ ์
์บ์ํฌ๊ธฐ(cacheSize) | ๋์์ด๋ฆ(cities) | ์คํ์๊ฐ |
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", "Rome", "Paris", "Jeju", "NewYork", "Rome"] | 60 |
5 | ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"] | 52 |
2 | ["Jeju", "Pangyo", "NewYork", "newyork"] | 16 |
0 | ["Jeju", "Pangyo", "Seoul", "NewYork", "LA"] | 25 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์์. ๋์ ํด์ค์ด ์์.
ํ์ด
- ๋จผ์ ์บ์ ๊ต์ฒด ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ฌ์ฉํ๋ LRU(Least Recently Used)๊ฐ ๋ญ์ง ํ์ธํ๋ค.
- ๊ฐ์ฅ ์ค๋ ์๊ฐ ์ฌ์ฉ๋์ง ์์ ํ์ด์ง๋ฅผ ๊ต์ฒดํ๋ ์ด์์ฒด์ ์ ํ์ด์ง ๊ต์ฒด ์ ์ฑ ์ค ํ๋
- Q ์คํ์ผ ์๋ฃ๊ตฌ์กฐ์ธ๊ฐ?
- ์บ์ ์ฌ์ฉํ ๋๋ ์์์ ๋ถํฐ ๊ฒ์.
- ์์ผ๋ฉด ์์ชฝ์ผ๋ก ๋ฐ์ดํฐ ๋ฐ์ด๋ฃ๊ณ ... ์บ์ ์ฌ์ด์ฆ๋ฅผ ๋์ด๊ฐ๋ ๊ฑด ๋ฒ๋ฆฌ๋ ๋ฐฉ์.
- LRU ์บ์๋ฅผ ๊ทธ๋๋ก ๊ตฌํํด์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ณ ๋นผ๋ฉด์ ์นด์ดํ
์ ํ๋ ์์ผ๋ก ๊ตฌํํ๋ฉด ๋๋ ๊ฑด๊ฐ?
- ์ง์ง ๊ตฌํ ๋ฌธ์ ์ธ๊ฐ? ์ค๋ง...? ์ ๋ง?
- ๊ทธ๋ ๋ฌด์ง์ฑ ๊ตฌํ ๊ฐ์ฆ์!
from collections import deque
def solution(cacheSize, cities):
answer = 0
cache = deque(["" for _ in range(cacheSize)])
for city in cities:
city = city.lower()
if cache.count(city): #cache hit
index = cache.index(city)
answer += 1
else: # cache miss
cache.appendleft(city)
cache.pop()
answer += 5
return answer
- ํ ์ง์ง ๊ฐ?
ํ
์คํธ 1
์
๋ ฅ๊ฐ ใ 3, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
๊ธฐ๋๊ฐ ใ 50
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 2
์
๋ ฅ๊ฐ ใ 3, ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"]
๊ธฐ๋๊ฐ ใ 21
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 3
์
๋ ฅ๊ฐ ใ 2, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
๊ธฐ๋๊ฐ ใ 60
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 4
์
๋ ฅ๊ฐ ใ 5, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
๊ธฐ๋๊ฐ ใ 52
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 5
์
๋ ฅ๊ฐ ใ 2, ["Jeju", "Pangyo", "NewYork", "newyork"]
๊ธฐ๋๊ฐ ใ 16
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 6
์
๋ ฅ๊ฐ ใ 0, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
๊ธฐ๋๊ฐ ใ 25
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
- ์ฑ์ ๊ฐ์ฆ์!
- ๋ญ์ผ? ์คํจํ๋๋?
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 9.98MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.4MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 11 ใ ์คํจ (126.20ms, 17.6MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.09ms, 10.2MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.07ms, 10.1MB)
ํ
์คํธ 15 ใ ์คํจ (0.21ms, 10.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.17ms, 10.2MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.38ms, 10.4MB)
ํ
์คํธ 18 ใ ์คํจ (0.34ms, 10.2MB)
ํ
์คํธ 19 ใ ์คํจ (0.84ms, 10.3MB)
ํ
์คํธ 20 ใ ์คํจ (0.64ms, 10.2MB)
- ์... ์์๋ค.
- ์บ์ ํํธ ํ๋๋ผ๋, ์บ์ ์์น๋ฅผ ๋ค์ ์์ผ๋ก ์ฎ๊ฒจ์ค์ผ ํ๋๊ตฌ๋?
- ์ด๋ฌ๋ ๊ฐ๋๋ฆฌ์ง ใ ก.ใ ก;
from collections import deque
def solution(cacheSize, cities):
answer = 0
cache = deque(["" for _ in range(cacheSize)])
for city in cities:
city = city.lower()
if cache.count(city): #cache hit
index = cache.index(city)
cache.remove(city)
cache.appendleft(city)
answer += 1
else: # cache miss
cache.appendleft(city)
cache.pop()
answer += 5
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 11 ใ ํต๊ณผ (88.27ms, 17.5MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.09ms, 10.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.15ms, 10.1MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.11ms, 10.2MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.14ms, 10.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.42ms, 10MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.71ms, 10.2MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.99ms, 10.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (1.03ms, 10.3MB)
- ๊ณ ์์ ํ์ด.
- ์บ์ ์ฌ์ด์ฆ๋ฅผ ๋ฏธ๋ฆฌ ์ง์ ํ ์ ์์ต๋๋ค. ํด๋จผ.
- cache.index(city)๋ก ์์น๋ฅผ ๊ฐ์ ธ์ฌ ํ์๊ฐ ์์ต๋๋ค. ํด๋จผ
- ๋ณด๊ณ ๋ฐฐ์ฐ์ญ์์ค. ํด๋จผ.
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
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.01ms, 9.99MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (85.69ms, 17.5MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.04ms, 10.1MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.04ms, 10MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.12ms, 10.2MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.17ms, 10.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.22ms, 10.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.14ms, 10.2MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.54ms, 10.3MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.43ms, 10.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.59ms, 10.3MB)
- ๋ ๋ค๋ฅธ ๊ณ ์, ๋ํ ์์จ๋ ๋น ๋ฅด๋ค...
def solution(cacheSize, cities):
answer = 0
stack = []
for x in map(lambda x: x.lower(), cities):
if x in stack:
answer += 1
stack.remove(x)
else:
answer += 5
stack += [x]
if len(stack) > cacheSize:
del stack[0]
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.4MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 11 ใ ํต๊ณผ (69.60ms, 17.6MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.15ms, 10.1MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.11ms, 10.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.15ms, 10.2MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.18ms, 10.2MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.31ms, 10.2MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.44ms, 10.2MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.59ms, 10.3MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.71ms, 10.1MB)
- ์นด์นด์ค ๋ฌธ์ ๋ค์ ๋ฌธ์ ์ค๋ช ์ด ์ด์ํ๋ค.
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค [1์ฐจ] ๋ด์คํด๋ฌ์คํฐ๋ง (0) | 2023.02.21 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2023.02.21 |
ํ๋ก๊ทธ๋๋จธ์ค ์คํฌํธ๋ฆฌ (0) | 2023.02.20 |
ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฉ๋ฌธ ๊ธธ์ด (0) | 2023.02.20 |
ํ๋ก๊ทธ๋๋จธ์ค ํ ์ธ ํ์ฌ (0) | 2023.02.20 |