๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿบ ๋งค์ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

2018 ์นด์นด์˜ค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ 1์ฐจ ์บ์‹œ JS ๋ฌธ์ œํ’€์ด

๐Ÿ”—์ง์ ‘ ๋ฌธ์ œ ํ’€๋Ÿฌ๊ฐ€๊ธฐ

 

์ž…๋ ฅ

  • ์บ์‹œํฌ๊ธฐ n๊ณผ ๋„์‹œ์ด๋ฆ„ ๋ฐฐ์—ด cities๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • 0 <= n <= 30
  • cities.length <= 100,000
  • ๊ฐ ๋„์‹œ์ด๋ฆ„์€ ์˜๋ฌธ์ž๋กœ๋งŒ ์ด๋ค„์ ธ์žˆ๊ณ , ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

  • ๋„์‹œ์ด๋ฆ„ ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ํ•„์š”ํ•œ ์ด ์‹คํ–‰์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์กฐ๊ฑด

  • ์บ์‹œ ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์ฐธ์กฐ๋œ ํŽ˜์ด์ง€๋ฅผ ์บ์‹œ์— ๋‹ด๊ณ ์žˆ๋Š” LRU(Least Recently Used)๋ฅผ ์‚ฌ์šฉ.
  • cache hit ์ผ ๊ฒฝ์šฐ ์‹คํ–‰์‹œ๊ฐ„์€ 1
  • cache miss ์ผ ๊ฒฝ์šฐ ์‹คํ–‰์‹œ๊ฐ„์€ 5

 

๋ฌธ์ œํ’€์ด

 

 

 

m = cities.length

n = 0 <= cache.size <= 30

์‹œ๊ฐ„๋ณต์žก๋„: O(nm) = O(m)

๊ณต๊ฐ„๋ณต์žก๋„: O(n) = O(1)

 

 

 

๋ฌธ์ œ์—์„œ n์ด 30์ดํ•˜๋กœ ์ œํ•œ๋˜์–ด์žˆ์–ด์„œ ์ƒ์ˆ˜ ์ทจ๊ธ‰ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋งŒ์•ฝ n์˜ ํฌ๊ธฐ๊ฐ€ ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•  ๋งŒํผ ํฌ๊ฑฐ๋‚˜ ํฌ๊ธฐ์˜ ์ œํ•œ์ด ์—†๋‹ค๋ฉด ์บ์‰ฌ๋ฅผ ๋ฐฐ์—ด์ด ์•„๋‹Œ ์ง‘ํ•ฉ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ํ›จ์”ฌ ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง‘ํ•ฉ์œผ๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

 

.values()๋Š” ์‚ฝ์ž…๋œ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” iterator object์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

m = cities.length

n =  0 <= cache.size <= 30

์‹œ๊ฐ„๋ณต์žก๋„: O(m) 

๊ณต๊ฐ„๋ณต์žก๋„: O(n) = O(1)