ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

 

๋ฌธ์ œํ’€์ด

LRU(Least Recently Used) ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์บ์‹œ(Cache)์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด  ์‚ฌ์šฉ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์บ์‹œ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฆฌ์†Œ์Šค์˜ ํฌ๊ธฐ๋Š” ์ œํ•œ๋˜์–ด ์žˆ์Œ
  • ์บ์‹œ๋Š” ์ œํ•œ๋œ ๋ฆฌ์†Œ์Šค ๋‚ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ €์žฅ ๋ฐ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ
  • ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋œ ์ ์ด ์—†๋Š” ์บ์‹œ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ถ€ํ„ฐ ๋Œ€์ฒดํ•˜๋ฉฐ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ 

์ฝ”๋“œ

function solution(s, w) {
  let answer = Array.from({length: s});

  for(let i = 0; i < w.length; i++) {
    if(!answer.includes(w[i])) { //Cache Miss์ผ ๊ฒฝ์šฐ
      if(answer.length === s) { //๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ชจ๋‘ ์ฐผ์„ ๊ฒฝ์šฐ
        answer.pop(); //๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋œ ์  ์—†๋Š” ๋ฐ์ดํ„ฐ ์‚ญ์ œ
        answer.unshift(w[i]);
      } else {  
        answer.unshift(w[i]);
      }
    } else { //Cache Hit์ผ ๊ฒฝ์šฐ
      let pos = answer.indexOf(w[i]); //ํ•ด๋‹น ์ž‘์—…๊ณผ ๋™์ผํ•˜๋ฉด์„œ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์ ์žฌ๋œ ๋ฐ์ดํ„ฐ ์œ„์น˜๋ฅผ ํƒ์ƒ‰
      for(let j = pos; j > 0; j--) {
        [answer[j - 1], answer[j]] = [answer[j], answer[j - 1]];
      }
    }
  }
  
  return answer;
}

let size = 5;
let work = [1, 2, 3, 2, 6, 2, 3, 5, 7]
console.log(solution(size, work));

function solution(s, w) {
  let answer = Array.from({length: s});

  w.forEach(x => {
    let pos = -1;
    for(let i = 0; i < size; i++) {
      if(answer[i] === x) pos = i;
    }
    if(pos === -1) {
      answer.unshift(x);
      if(answer.length > s) answer.pop();
    }
    else {
      answer.splice(pos, 1); //ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋“ค์€ ํ•œ์นธ์”ฉ ๋’ค๋กœ ์ด๋™
      answer.unshift(x);
    }
  })
  
  return answer;
}

let size = 5;
let work = [1, 2, 3, 2, 6, 2, 3, 5, 7]
console.log(solution(size, work));

 

์ฝ”๋“œ_์‚ฝ์ž… ์ •๋ ฌ

function solution(s, w) {
  let answer = Array.from({length: s});

  w.forEach(x => {
    let pos = -1;
    for(let i = 0; i < s; i++) {
      if(answer[i] === x) { //Cache Hit์ผ ๊ฒฝ์šฐ
        pos = i; //ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜ ์ €์žฅ
      }
    }
    if(pos === -1){ //Cache Miss์ผ ๊ฒฝ์šฐ
      for(let i = s - 1; i >= 1; i--) { //๋งจ ๋’ค์—์„œ๋ถ€ํ„ฐ ๋งจ ์•ž ์ด์ „๊นŒ์ง€ ์ˆœํšŒ
        answer[i] = answer[i - 1]; //์‚ฝ์ž… ์ •๋ ฌ ์ˆ˜ํ–‰
      }
    }
    else {
      for(let i = pos; i >= 1; i--) { //Cache Hit์ผ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ถ€ํ„ฐ ์ˆœํšŒ
        answer[i] = answer[i - 1];
      }
    }
    answer[0] = x; //๋ฉ”๋ชจ๋ฆฌ ์ƒ ๊ฐ€์žฅ ๋งจ ์•ž์— ์ €์žฅ(์‚ฝ์ž… ์ •๋ ฌ๋กœ ์ธํ•ด ๊ฐ€์žฅ ๋งจ ์•ž์€ ๋น„์–ด์ ธ ์žˆ์Œ)
  })
  
  return answer;
}

let size = 5;
let work = [1, 2, 3, 2, 6, 2, 3, 5, 7]
console.log(solution(size, work));

 

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > ํƒœ๊ทธ ๋ณ„ ํ’€์ด' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ขŒํ‘œ ์ •๋ ฌ  (0) 2021.07.22
์žฅ๋‚œ๊พธ๋Ÿฌ๊ธฐ ํ˜„์ˆ˜  (0) 2021.07.21
์‚ฝ์ž… ์ •๋ ฌ  (0) 2021.07.19
Special Sort  (0) 2021.07.18
๋ฒ„๋ธ” ์ •๋ ฌ  (0) 2021.07.18
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ