File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments