์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : Least Recently Used

2023. 11. 6. 10:17ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1242

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์‚ฝ์ž… ์ •

https://hyejin.tistory.com/1241 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ๋ฒ„๋ธ” ์ • https://hyejin.tistory.com/1240 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searc

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

4. Least Recently Used

์„ค๋ช…

์บ์‹œ๋ฉ”๋ชจ๋ฆฌ๋Š” CPU์™€ ์ฃผ๊ธฐ์–ต์žฅ์น˜(DRAM) ์‚ฌ์ด์˜ ๊ณ ์†์˜ ์ž„์‹œ ๋ฉ”๋ชจ๋ฆฌ๋กœ์„œ CPU๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ž‘์—…์„ ์ €์žฅํ•ด ๋†“์•˜๋‹ค๊ฐ€

ํ•„์š”ํ•  ๋ฐ”๋กœ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌ์†๋„๋ฅผ ๋†’์ด๋Š” ์žฅ์น˜์ด๋‹ค. ์›Œ๋‚™ ๋น„์‹ธ๊ณ  ์šฉ๋Ÿ‰์ด ์ž‘์•„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ฒ ์ˆ˜์˜ ์ปดํ“จํ„ฐ๋Š” ์บ์‹œ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ๊ทœ์น™์ด LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋”ฐ๋ฅธ๋‹ค.

 

LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Least Recently Used ์˜ ์•ฝ์ž๋กœ ์ง์—ญํ•˜์ž๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ ์ •๋„์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์บ์‹œ์—์„œ ์ž‘์—…์„ ์ œ๊ฑฐํ•  ๋•Œ ๊ฐ€์žฅ ์˜ค๋žซ๋™์•ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์ œ๊ฑฐํ•˜๊ฒ ๋‹ค๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

 

 

์บ์‹œ์˜ ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ์บ์‹œ๊ฐ€ ๋น„์–ด์žˆ๋Š” ์ƒํƒœ์—์„œ N๊ฐœ์˜ ์ž‘์—…์„ CPU๊ฐ€ ์ฐจ๋ก€๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด N๊ฐœ์˜ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„

์บ์‹œ๋ฉ”๋ชจ๋ฆฌ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์žฅ ์ตœ๊ทผ ์‚ฌ์šฉ๋œ ์ž‘์—…๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์บ์‹œ์˜ ํฌ๊ธฐ์ธ S(3<=S<=10)์™€ ์ž‘์—…์˜ ๊ฐœ์ˆ˜ N(5<=N<=1,000)์ด ์ž…๋ ฅ๋œ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์ž‘์—…๋ฒˆํ˜ธ๊ฐ€ ์ฒ˜๋ฆฌ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ž‘์—…๋ฒˆํ˜ธ๋Š” 1 ~100 ์ด๋‹ค.

 

์ถœ๋ ฅ

๋งˆ์ง€๋ง‰ ์ž‘์—… ํ›„ ์บ์‹œ๋ฉ”๋ชจ๋ฆฌ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์žฅ ์ตœ๊ทผ ์‚ฌ์šฉ๋œ ์ž‘์—…๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

5 9
1 2 3 2 6 2 3 5 7

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

7 5 3 2 6

 

ํžŒํŠธ

 

 

๋ฌธ์ œ ํ’€์ด 1

public ArrayList<Integer> solution(int s, int n, int[] arr)
{
   ArrayList<Integer> answer = new ArrayList<>();
   for (int i = 0; i < s-1; i++)
   {
      answer.add(0);
   }
   
   for (int i : arr)
   {
      if (answer.size() < s) {
         if (!answer.contains(i)) {
            answer.add(0,i);
         }else
         {
            answer.remove(Integer.valueOf(i));
            answer.add(0, i);
         }
      }else
      {
         if (answer.contains(i)) {
            answer.remove(Integer.valueOf(i));
            answer.add(0, i);
         }else
         {
            answer.remove(s-1);
            answer.add(0, i);
         }
      }
   }
   
   return answer;
}

 

๐Ÿ‘ฉ‍๐Ÿ’ป : ๋‚˜๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ Arraylist๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ... ใ…Ž ๊ฐ•์‚ฌ๋‹˜์ด ๊ทธ๋ ‡๊ฒŒ ํ’€์ง€ ๋ง๋ผ๊ณ  ํ•˜์…จ๋‹ค ํ•˜ํ•ณ 

์ด์ „์— ์‚ฝ์ž… ์ •๋ ฌ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ์œผ๋‹ˆ ์‚ฝ์ž… ์ •๋ ฌ์„ ์ด์šฉํ•˜๋ผ๊ณ ..! 

 

์šฐ์„  ์ด๊ฒƒ๋„ ์ •๋‹ต์€ ์ •๋‹ต์ด์—ˆ์œผ๋‹ˆ.. ๋‚˜์˜ ํ’€์ด์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. 

ArrayList<Integer> answer = new ArrayList<>();
for (int i = 0; i < s-1; i++)
{
   answer.add(0);
}

-> Arraylist๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ฃผ์–ด์ง„ ํฌ๊ธฐ s -1 ์ „๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฉด์„œ answer์— 0 ๊ฐ’์„ ๋„ฃ์–ด์คฌ๋‹ค. 

 

if (answer.size() < s) {
   if (!answer.contains(i)) {
      answer.add(0,i);
   }else
   {
      answer.remove(Integer.valueOf(i));
      answer.add(0, i);
   }
}

-> ๊ทธ ๋‹ค์Œ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด arr์„ ๋ฐ˜๋ณต๋ฌธ ๋„๋Š”๋ฐ, ์ด๋•Œ answer์—  sizer๊ฐ€ ์ฃผ์–ด์ง„ ํฌ๊ธฐ s๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ i๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด answer์— addํ•  ๋•Œ ์ธ๋ฑ์Šค ์œ„์น˜๋ฅผ 0์œผ๋กœ ์ง€์ •ํ•ด์„œ i ๊ฐ’์„ ๋„ฃ์–ด์คฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  i๊ฐ€  ํฌํ•จ๋œ๋‹ค๋ฉด answer  ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹น ๊ฐ’์„ remove ํ•ด์คฌ๊ณ , ๊ทธ ๋‹ค์Œ ๋‹ค์‹œ answer ๋ฆฌ์ŠคํŠธ์— ์ธ๋ฑ์Šค ์œ„์น˜๋Š” 0์œผ๋กœ ์ง€์ •ํ•ด์„œ i ๊ฐ’์„ addํ–ˆ๋‹ค. 

 

else
{
   if (answer.contains(i)) {
      answer.remove(Integer.valueOf(i));
      answer.add(0, i);
   }else
   {
      answer.remove(s-1);
      answer.add(0, i);
   }
}

-> ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ answer์˜ ํฌ๊ธฐ๊ฐ€ s๋ณด๋‹ค ํด ๊ฒฝ์šฐ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๊ฝ‰ ์ฐผ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ i ๊ฐ€ answer ๋ฆฌ์ŠคํŠธ์— ์ด๋ฏธ ์žˆ๋Š” ๊ฐ’์ด๋ผ๋ฉด ์•ž์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ answer์— ๊ทธ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‹ค์‹œ ์ธ๋ฑ์Šค์˜ ์œ„์น˜๊ฐ€ 0 ์— i ๊ฐ’์„ add ํ–ˆ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ answer์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๊ฐ’์ด๋ผ๋ฉด answer์— s-1 ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ remove ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ ์ธ๋ฑ์Šค ์œ„์น˜ 0์— i ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

ํ•˜๊ธด.. ์ด ๋ฌธ์ œ ํ’€๋ฉด์„œ๋„ ์ด๊ฑธ ์™œ ์ด ์„น์…˜ 6์—๋‹ค ๋„ฃ์—ˆ์ง€..? ํ•˜๋ฉด์„œ  ํ’€์—ˆ๋Š”๋ฐ ..ใ…Žใ…Ž ์—ญ์‹œ๋‚˜ Arraylist๋กœ ํ’€๋„๋ก ๋‚ธ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์—ˆ๋‹คใ…œ 

 

 

๋ฌธ์ œ ํ’€์ด 2

public int[] solution2(int size, int n, int[] arr)
{
   int[] cache = new int[size];
   for (int x : arr)
   {
      int pos = -1; // ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ (์œ„์น˜)
      for (int i = 0; i< size; i++)
      {
         if (x == cache[i]) { // hit !
            pos = i;
         }
      }
      if (pos == -1) { // miss ์ƒํ™ฉ
         for (int i = size-1; i >= 1; i--)
         {
            cache[i] = cache[i - 1];
         }
      }else // hit ์ฒ˜๋ฆฌ
      {
         for (int i = pos; i >= 1; i--)
         {
            cache[i] = cache[i - 1];
         }
      }
      cache[0] = x;
   }
   
   
   return cache;
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์€ ์ด ๋ฌธ์ œ๋ฅผ Arraylist๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ํ’€๋ผ๊ณ  ํ•˜์…จ๊ธฐ ๋•Œ๋ฌธ์— cache๋ผ๋Š” ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง„ size ์ธ int ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค. 

 

๊ทธ ๋‹ค์Œ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด arr๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋„๋Š”๋ฐ ์ด๋•Œ pos ๋ผ๋Š” ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๊ณ  ์ด ๋ณ€์ˆ˜๋Š” hit ๋œ ์ธ๋ฑ์Šค์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜์ด๋‹ค. ๋จผ์ € cache ๋ฐฐ์—ด์— x ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์„ ๋Œ๋ฉด์„œ x == cache[i] ์ด๋ฉด ๋ฉ”๋ชจ๋ฆฌ์— x ๊ฐ’์ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ pos์— i ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค. 

 

๋ฐ˜๋ณต๋ฌธ์„ ๋‹ค ๋Œ๊ณ  ๋‚˜์„œ ๋งŒ์•ฝ pos๊ฐ€ -1์ด๋ผ๋Š” ๊ฑด hit๋œ ๊ฐ’์ด ์—†๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐฐ์—ด์— ๋„ฃ์–ด์•ผ ํ•˜๋Š”๋ฐ 

์ด๋•Œ ๋„ฃ์„ ๋•Œ๋Š” ์˜†์œผ๋กœ(์˜ค๋ฅธ์ชฝ์œผ๋กœ) ๊ฐ’์„ ๋ฐ€๋ฉด์„œ ๋‚˜์•„๊ฐ€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 

if (pos == -1) { // miss ์ƒํ™ฉ
   for (int i = size-1; i >= 1; i--)
   {
      cache[i] = cache[i - 1];
   }
}

for๋ฌธ์„ ๋Œ ๋•Œ i๊ฐ€ size-1 ๋ถ€ํ„ฐ 1๊นŒ์ง€๋งŒ ๋Œ๋ฉด์„œ cache[i]์— ๊ฐ’์„ cache[i-1] ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. 

 

else // hit ์ฒ˜๋ฆฌ
{
   for (int i = pos; i >= 1; i--)
   {
      cache[i] = cache[i - 1];
   }
}

๋˜๋Š” pos๊ฐ€ -1์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฑด hit๋๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ for๋ฌธ ๋ฐ˜๋ณต๋ฌธ์„ hit๋œ ์œ„์น˜ pos ๋ถ€ํ„ฐ 1๊นŒ์ง€๋งŒ ๋Œ๋ฉด์„œ ๊ฐ’์„ ๋ฐ€์–ด์ค€๋‹ค. 

 

cache[0] = x;

๋ฐ˜๋ณต์„ ๋งˆ์น˜๊ณ ๋Š” cache[0]์— x ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. (์ตœ์‹  ๊ฐ’) 

 

 

 

 

 

 

 

728x90

'์ธํ”„๋Ÿฐ > ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์žฅ๋‚œ ๊พธ๋Ÿฌ๊ธฐ  (1) 2023.11.08
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ค‘๋ณต ํ™•์ธ  (0) 2023.11.07
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์‚ฝ์ž… ์ •๋ ฌ  (0) 2023.11.06
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ๋ฒ„๋ธ” ์ •๋ ฌ  (1) 2023.11.03
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์„ ํƒ ์ •๋ ฌ  (0) 2023.11.03