Skip to content

Commit

Permalink
#7 22.12.02 > LRU (내장함수로 풀기)
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Dec 2, 2022
1 parent 815c331 commit 505e6db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/inf/sort,greedy/5_re.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

function solution(size, arr) {
let answer = [];
arr.forEach((nowValue) => {
let pos = -1;
for (let i = 0; i < size; i++) {
if (nowValue === answer[i]) pos = i; // 캐시 히트인 경우 index를 pos에 저장
}
if (pos === -1) {
// 캐시 미스인 경우
answer.unshift(nowValue);
if (answer.length > size) answer.pop();
} else {
// 캐시 히트인 경우
answer.splice(pos, 1);
answer.unshift(nowValue);
}
});
return answer;
}

let arr = [1, 2, 3, 2, 6, 2, 3, 5, 7];
console.log(solution(5, arr));
2 changes: 1 addition & 1 deletion src/inf/sort,greedy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| 2 | O | 22.10.30😥 |
| 3 | O | 22.10.30 |
| 4 | X | 22.10.30😥 |
| 5 | 몰라 | 22.12.01😥 |
| 5 | 몰라 | 22.12.02😥 |
| 6 | O | 22.11.01 |
| 7 || 22.11.01 |
| 8 || 22.11.06 |
Expand Down

0 comments on commit 505e6db

Please sign in to comment.