-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path캐시.js
41 lines (32 loc) · 839 Bytes
/
캐시.js
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
/*
캐시 크기는 최대 30 * 도시 10만개
매번 캐시를 순회해서 LRU를 찾더라도 충분히 구현 가능할듯?
*/
class Cache {
constructor(size) {
this.cache = [];
this.size = size;
}
find(target) {
if (this.size === 0) return 5;
const index = this.cache.findIndex((value) => value === target);
if (index >= 0) {
this.cache = [...this.cache.slice(0, index), ...this.cache.slice(index + 1), target];
return 1;
}
if (this.cache.length < this.size) {
this.cache.push(target);
return 5;
}
this.cache = [...this.cache.slice(1), target];
return 5;
}
}
function solution(cacheSize, cities) {
let time = 0;
const cache = new Cache(cacheSize);
cities.forEach((city) => {
time += cache.find(city.toLowerCase());
});
return time;
}