Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 025311c

Browse files
[1차]-캐시 파일이름공백 제거 및 add solution 1
1 parent 70e928d commit 025311c

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

‎level-2/[1차]-캐시.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
function solution(cacheSize, cities) {
5+
var answer = 0;
6+
let cache = []
7+
if (cacheSize === 0) return 5 * cities.length
8+
for (const city of cities) {
9+
const cityLC = city.toLowerCase()
10+
if (cache.includes(cityLC)) {
11+
cache.splice(cache.indexOf(cityLC), 1)
12+
cache.unshift(cityLC)
13+
answer += 1
14+
} else {
15+
if (cache.length >= cacheSize) cache.pop()
16+
cache.unshift(cityLC)
17+
answer += 5
18+
}
19+
}
20+
return answer;
21+
}
22+
23+
//정답 2 - jaewon1676
24+
function solution(cacheSize, cities) {
25+
var answer = 0;
26+
let cache = [];
27+
28+
//캐시 크기가 0인 경우는 따로 처리
29+
if (cacheSize === 0) return cities.length * 5;
30+
31+
while (cities.length != 0) {
32+
// 맨 앞의 배열의 요소 히나를 소문자로 변환해서 city에 넣는다.
33+
const city = cities.shift().toLowerCase();
34+
// cities의 요소 city가 캐시 안에 있는지 비교한다. (hit or miss)
35+
if (cache.includes(city)) {
36+
// 캐시 안에 있으면 그 위치에 있는 캐시를 빼주고,
37+
cache.splice(cache.indexOf(city), 1);
38+
// 맨 뒤로 push 해준다.
39+
cache.push(city);
40+
// cache hit
41+
answer += 1;
42+
} else { // 캐시 크기가 꽉 차있으면 캐시 맨 앞에 요소를 하나 빼준다.
43+
if (cache.length === cacheSize) {
44+
cache.shift();
45+
}
46+
// 새로운 캐시 맨 뒤로 push
47+
cache.push(city);
48+
// cache miss
49+
answer += 5;
50+
}
51+
}
52+
return answer;
53+
}
54+
/* LRU 알고리즘
55+
n이 배열 안에 있으면 배열 안의 n을 빼주고, 새 n을 배열의 맨 뒤로 push 한다.
56+
n이 배열 안에 없으면 n을 배열의 맨 뒤로 push 한다. 이때 배열의 크기 여유가 없으면
57+
배열에서 가장 오래된 요소를 하나 뺴준다. (arr.shift()) */
58+

‎level-2/[1차]-캐시.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /