-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path์บ์.js
66 lines (53 loc) ยท 1.26 KB
/
์บ์.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Solution 1
function solution1(cacheSize, cities) {
const MISS_RUNTIME = 5;
const HIT_RUNTIME = 1;
const _cities = cities.map((city) => city.toLowerCase());
const cache = [];
let totalRuntime = 0;
if (cacheSize === 0) {
return cities.length * MISS_RUNTIME;
}
for (const city of _cities) {
if (cache.includes(city)) {
const index = cache.indexOf(city);
cache.splice(index, 1);
cache.push(city);
totalRuntime += HIT_RUNTIME;
} else {
if (cache.length < cacheSize) {
cache.push(city);
} else {
cache.shift();
cache.push(city);
}
totalRuntime += MISS_RUNTIME;
}
}
return totalRuntime;
}
// Solution 2
function solution2(cacheSize, cities) {
const MISS_RUNTIME = 5;
const HIT_RUNTIME = 1;
const cache = [];
let totalRuntime = 0;
if (cacheSize === 0) {
return cities.length * MISS_RUNTIME;
}
cities.forEach((city) => {
city = city.toUpperCase();
const index = cache.indexOf(city);
if (index > -1) {
cache.splice(index, 1);
totalRuntime += HIT_RUNTIME;
} else {
if (cache.length >= cacheSize) {
cache.shift();
}
totalRuntime += MISS_RUNTIME;
}
cache.push(city);
});
return totalRuntime;
}