[JavaScript/section 7] 05 - LRU
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ 05 - Least Recently Used(์นด์นด์˜ค ์บ์‹œ ๋ฌธ์ œ ๋ณ€ํ˜•) ๋งŒ์•ฝ ์บ์‹œ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 5์ด๊ณ  ์ž‘์—…์ด [2, 3, 1, 6, 7] ์ˆœ์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์žโ— 1) Cache Miss: ํ•ด์•ผํ•  ์ž‘์—…์ด ์บ์‹œ์— ์—†๋Š” ์ƒํƒœ๋กœ ์œ„ ์ƒํƒœ์—์„œ ๋งŒ์•ฝ ์ƒˆ๋กœ์šด ์ž‘์—…์ธ 5๋ฒˆ ์ž‘์—…์„ CPU๊ฐ€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Cache Miss๊ฐ€ ๋˜๊ณ  ๋ชจ๋“  ์ž‘์—…์ด ๋’ค๋กœ ๋ฐ€๋ฆฐ ํ›„ 5๋ฒˆ ์ž‘์—…์€ ์บ์‹œ์˜ ๋งจ ์•ž์— ์œ„์น˜ํ•œ๋‹ค. [5, 2, 3, 1, 6] (7๋ฒˆ ์ž‘์—…์€ ์บ์‹œ์—์„œ ์‚ญ์ œ๋œ๋‹ค.) 2) Cache Hit: ํ•ด์•ผํ•  ์ž‘์—…์ด ์บ์‹œ์— ์žˆ๋Š” ์ƒํƒœ๋กœ ์œ„ ์ƒํƒœ์—์„œ ๋งŒ์•ฝ 3๋ฒˆ ์ž‘์—…์„ CPU๊ฐ€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Cache Hit์ด ๋˜๊ณ , 3๋ฒˆ ์•ž์— ์žˆ๋Š” 5, 2๋ฒˆ ์ž‘์—…์€ ํ•œ ์นธ ๋’ค๋กœ ๋ฐ€๋ฆฐ ํ›„ 3๋ฒˆ ์ž‘์—…์€ ์บ์‹œ์˜ ๋งจ ์•ž์— ์œ„์น˜ํ•œ๋‹ค. [3, 5, 2, 1, 6..
[JavaScript/section 7] 04 - ์‚ฝ์ž… ์ •๋ ฌ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ N๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์‚ฝ์ž… ์ •๋ ฌ์ด๋‹ค. ๐Ÿง ์‚ฝ์ž… ์ •๋ ฌ(insertion sort)์ด๋ž€ ๋ฌด์—‡์ผ๊นŒโ“ ์† ์•ˆ์˜ ์นด๋“œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•˜๋‹ค. ์ƒˆ๋กœ์šด ์นด๋“œ๋ฅผ ๊ธฐ์กด์˜ ์ •๋ ฌ๋œ ์นด๋“œ ์‚ฌ์ด์˜ ์˜ฌ๋ฐ”๋ฅธ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„ ์‚ฝ์ž…ํ•œ๋‹ค. ์ƒˆ๋กœ ์‚ฝ์ž…๋  ์นด๋“œ์˜ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜๋ฉด ์ „์ฒด ์นด๋“œ๊ฐ€ ์ •๋ ฌ๋œ๋‹ค. ์ž๋ฃŒ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ด๋ฏธ ์ •๋ ฌ๋œ ๋ฐฐ์—ด ๋ถ€๋ถ„๊ณผ ๋น„๊ตํ•˜์—ฌ, ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ์‚ฝ์ž…ํ•จ์œผ๋กœ์จ ์ •๋ ฌ์„ ์™„์„ฑํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งค ์ˆœ์„œ๋งˆ๋‹ค ํ•ด๋‹น ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ฐพ์•„ ํ•ด๋‹น ์œ„์น˜์— ๋„ฃ๋Š”๋‹ค. ๐Ÿ“ ํ’€์ด ๐Ÿง‘๐Ÿป‍๐Ÿ’ป ๋‚˜์˜ ํ’€์ด ๋ฐฉ๋ฒ• function solution(arr) { const answer = []; for (let i = 0; i < arr.l..
_์„ฑํ˜ธ_
'์‚ฝ์ž… ์ •๋ ฌ' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก