문제 링크 : 코딩테스트 연습>2018 KAKAO BLIND RECRUITMENT>[1차] 캐시
cacheSize
: 캐시 크기cacheSize
≤30cities
: 도시 이름으로 이뤄진 문자열 배열LRU(Least Recently Used)
사용cache hit
: 실행 시간 1
cache miss
: 실행 시간 5
Miss
Hit
Cache Hit : CPU가 참조하고자하는 메모리가 캐시에 존재하는 경우
Cache Miss : CPU가 참조하고자하는 메모리가 캐시에 존재하지 않은 경우
Hit Ratio : Cache Hit / (Cache Hit + Cache Miss)
Miss Ratio : Cache Miss / (Cache Hit + Cache Miss)
※ 위의 예시 : Hit Ratio = 3/8, Miss Ratio = 5/8
function solution(cacheSize, cities) {
let answer = 0;
let LUR = [];
for (let city of cities) {
city = city.toLowerCase();
let i = LUR.indexOf(city);
if (i !== -1) {
LUR.splice(i, 1);
answer += 1;
} else {
answer += 5;
}
LUR.push(city);
if (LUR.length > cacheSize) LUR.shift();
}
return answer;
}
function solution(cacheSize, cities) {
let answer = 0;
let dll = new DLL();
for (let city of cities) {
city = city.toLowerCase();
let isNode = dll.get(city);
if (isNode) {
if (isNode.index !== 0) {
dll.remove(isNode.node);
dll.unshift(city);
}
answer += 1;
} else {
answer += 5;
dll.unshift(city);
}
if (dll.length > cacheSize) dll.pop();
}
return answer;
}
class Node {
constructor(val) {
this.val = val;
this.prev = null;
this.next = null;
}
}
class DLL {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
pop() {
let node = this.tail;
if (!this.head) return undefined;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
node.prev.next = null;
this.tail = node.prev;
node.prev = null;
}
this.length--;
return node;
}
unshift(val) {
let newNode = new Node(val);
if (this.length === 0) {
this.head = newNode;
this.tail = newNode;
} else {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
get(val) {
let index = 0;
let cur = this.head;
while (index < this.length) {
if (cur.val === val) return { 'node': cur, 'index': index };
cur = cur.next;
index++;
}
return null;
}
remove(node) {
if (this.length === 0) return undefined;
if (node.next === null) {
this.pop();
} else {
let removedNode = node;
let prevNode = node.prev;
let nextNode = node.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
removedNode.next = null;
removedNode.prev = null;
this.length--;
}
}
}