-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogrammers_cache.py
More file actions
56 lines (52 loc) Β· 2.73 KB
/
programmers_cache.py
File metadata and controls
56 lines (52 loc) Β· 2.73 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
μΊμ
μ§λκ°λ°νμμ 근무νλ μ μ΄μ§λ μ§λμμ λμ μ΄λ¦μ κ²μνλ©΄ ν΄λΉ λμμ κ΄λ ¨λ λ§μ§ κ²μλ¬Όλ€μ λ°μ΄ν°λ² μ΄μ€μμ μ½μ΄ 보μ¬μ£Όλ μλΉμ€λ₯Ό κ°λ°νκ³ μλ€.
μ΄ νλ‘κ·Έλ¨μ ν
μ€ν
μ
무λ₯Ό λ΄λΉνκ³ μλ μ΄νΌμΉλ μλΉμ€λ₯Ό μ€ννκΈ° μ κ° λ‘μ§μ λν μ±λ₯ μΈ‘μ μ μννμλλ°, μ μ΄μ§κ° μμ±ν λΆλΆ μ€ λ°μ΄ν°λ² μ΄μ€μμ κ²μλ¬Όμ κ°μ Έμ€λ λΆλΆμ μ€νμκ°μ΄ λ무 μ€λ κ±Έλ¦°λ€λ κ²μ μκ² λμλ€.
μ΄νΌμΉλ μ μ΄μ§μκ² ν΄λΉ λ‘μ§μ κ°μ νλΌκ³ λ¦λ¬νκΈ° μμνμκ³ , μ μ΄μ§λ DB μΊμλ₯Ό μ μ©νμ¬ μ±λ₯ κ°μ μ μλνκ³ μμ§λ§ μΊμ ν¬κΈ°λ₯Ό μΌλ§λ‘ ν΄μΌ ν¨μ¨μ μΈμ§ λͺ°λΌ λκ°ν μν©μ΄λ€.
μ΄νΌμΉμκ² μλ¬λ¦¬λ μ μ΄μ§λ₯Ό λμ, DB μΊμλ₯Ό μ μ©ν λ μΊμ ν¬κΈ°μ λ°λ₯Έ μ€νμκ° μΈ‘μ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ
λ ₯ νμ
μΊμ ν¬κΈ°(cacheSize)μ λμμ΄λ¦ λ°°μ΄(cities)μ μ
λ ₯λ°λλ€.
cacheSizeλ μ μμ΄λ©°, λ²μλ 0 β¦ cacheSize β¦ 30 μ΄λ€.
citiesλ λμ μ΄λ¦μΌλ‘ μ΄λ€μ§ λ¬Έμμ΄ λ°°μ΄λ‘, μ΅λ λμ μλ 100,000κ°μ΄λ€.
κ° λμ μ΄λ¦μ 곡백, μ«μ, νΉμλ¬Έμ λ±μ΄ μλ μλ¬Έμλ‘ κ΅¬μ±λλ©°, λμλ¬Έμ ꡬλΆμ νμ§ μλλ€. λμ μ΄λ¦μ μ΅λ 20μλ‘ μ΄λ£¨μ΄μ Έ μλ€.
μΆλ ₯ νμ
μ
λ ₯λ λμμ΄λ¦ λ°°μ΄μ μμλλ‘ μ²λ¦¬ν λ, "μ΄ μ€νμκ°"μ μΆλ ₯νλ€.
쑰건
μΊμ κ΅μ²΄ μκ³ λ¦¬μ¦μ LRU(Least Recently Used)λ₯Ό μ¬μ©νλ€.
cache hitμΌ κ²½μ° μ€νμκ°μ 1μ΄λ€.
cache missμΌ κ²½μ° μ€νμκ°μ 5μ΄λ€.
μ
λ ₯ μμ :
Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"
νμ΄κ³Όμ :
1. cach(Jeju) = Jeju(5)
2. cache(Pangyo) = Jeju", "Pangyo" (5)
3. cache(Seoul) = Jeju", "Pangyo", "Seoul" (5)
4. cache(NewYork) = "Pangyo", "Seoul", "NewYork" (5)
5. cache(LA) = "Seoul", "NewYork", "LA" (5)
6. cache(Jeju) = "NewYork", "LA", Jeju", (5)
7. cache(Pangyo) = "LA", Jeju" , Pangyo (5)
8. cache(Seoul) = "Jeju", "Pangyo", "Seoul" (5)
9. cache(NewYork) = "Pangyo", "Seoul", "NewYork" (5)
10. cache(LA) = "Seoul", "NewYork", "LA" (5)
κ²°κ³Ό :
μ€νμκ° = 50
"""
def solution(cacheSize, cities):
if cacheSize == 0 :
return len(cities) * 5
answer = 0
cache = []
for city in cities:
city = city.lower()
if city in cache :
answer += 1
cache.remove(city)
cache.append(city)
else :
answer += 5
if len(cache) < cacheSize :
cache.append(city)
else :
cache.pop(0)
cache.append(city)
return answer