Skip to content

Commit dc7e0d9

Browse files
committed
1 parent a99f0a0 commit dc7e0d9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
https://school.programmers.co.kr/learn/courses/30/lessons/17680
3+
"""
4+
5+
def solution(cacheSize, cities):
6+
answer = 0
7+
#1. ๋ชจ๋“  ๋„์‹œ์ด๋ฆ„ ์†Œ๋ฌธ์ž -> ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ
8+
for c in range(len(cities)):
9+
cities[c]=cities[c].upper()
10+
stack = []
11+
# +์˜ˆ์™ธ์ฒ˜๋ฆฌ : chachSize๊ฐ€ 0์ผ๋•Œ -> answer = 5*dB๊ฐœ์ˆ˜
12+
if cacheSize <= 0 :
13+
return (5*len(cities))
14+
# 2. City ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต
15+
for i in range(len(cities)):
16+
city = cities[i]
17+
cache_miss = True
18+
# 2-1 . Stack์— ์žˆ์„๋•Œ(cache hit)
19+
for s in range(len(stack)):
20+
if city == stack[s][1] : # stack์— ์žˆ์Œ
21+
answer += 1 # cache hit ์‹œ๊ฐ„ ์ถ”๊ฐ€
22+
cache_miss = False
23+
stack[s][0] = answer # ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ
24+
25+
#2-2. ์œ„ cache ํƒ์ƒ‰ ํ›„์—๋„ Cache miss ๋ฐœ์ƒ
26+
if cache_miss : # Cache miss ๋‚จ
27+
# [1] cache miss ๋ฐœ์ƒ& stack์ด not full
28+
answer += 5
29+
if len(stack) < cacheSize:
30+
stack.append([answer,city])
31+
#[2] cache miss ๋ฐœ์ƒ & stack ์ด full ํ•œ ๊ฒฝ์šฐ -> replace ๊ต์ฒด (recently cache)
32+
else :
33+
# stack๋‚ด์— ๊ฒ€์ƒ‰ ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ์ด๋ฅธ db ๋ฒ„๋ฆฌ๊ธฐ
34+
stack.sort(key = lambda x : x[0])
35+
stack.pop(0)
36+
# ์ƒˆ๋กœ์šด data๋กœ ๊ต์ฒด
37+
stack.append([answer,city])
38+
39+
return answer

0 commit comments

Comments
ย (0)