[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์บ์‹œ/ ํŒŒ์ด์ฌ/ Python/ deque/ LRU / 2018 KAKAO BLIND RECRUITMENT /์นด์นด์˜ค ์ฝ”ํ…Œ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์บ์‹œ/ ํŒŒ์ด์ฌ/ Python/ deque/ LRU / 2018 KAKAO BLIND RECRUITMENT /์นด์นด์˜ค ์ฝ”ํ…Œ

728x90
๋ฐ˜์‘ํ˜•

 

 

๐Ÿ’กsolutions )

LRU(Least Recently Used) ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ, ์˜ค๋žซ๋™์•ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋˜ ๋ฐ์ดํ„ฐ๋Š” ์•ž์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ํ™•๋ฅ ์ด ์ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ํ•œ์ •๋œ ์บ์‹œ ์‚ฌ์ด์ฆˆ๊ฐ€ ๊ฝ‰ ์ฐจ๊ณ  ์ƒˆ๋กœ์šด ์บ์‹œ๋ฅผ ๋„ฃ์œผ๋ ค๊ณ  ํ•  ๋•Œ, ๊ธฐ์กด ์บ์‹œ ์ค‘ ์ตœ๊ทผ๊นŒ์ง€ ๊ฐ€์žฅ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

 

๐Ÿ’ฌ ์ฒซ ๋ฒˆ์งธ if๋ฌธ -> cacheSize๊ฐ€ 0์ธ ๊ฒฝ์šฐ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ cities ๋ชจ๋“  ์š”์†Œ๊ฐ€ cache miss๋กœ ์‹คํ–‰์‹œ๊ฐ„์ด 5์ด๋‹ค.

๐Ÿ’ฌ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋‹ˆ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌ -> lower() ๋ฉ”์†Œ๋“œ

๐Ÿ’ฌ for๋ฌธ -> ๊ฐ city๊ฐ€ buffer์— ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๋Š”๋ฐ

โ‘  ์—†๋Š” ๊ฒฝ์šฐ buffer๊ฐ€ cacheSize๋งŒํผ ๊ฝ‰ ์ฐจ ์žˆ๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.

- cacheSize์™€ ๊ฐ™์œผ๋ฉด ์ƒˆ๋กœ์šด ์ฐธ์กฐ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ -> ์ œ์ผ ์˜ค๋ž˜ ์ „ ์ฐธ์กฐ๋œ ์š”์†Œ popleft()ํ•œ ํ›„ append()

- cacheSize ๋ณด๋‹ค ์ž‘์œผ๋ฉด -> ๊ทธ๋ƒฅ append()

- 1๋ฒˆ์˜ ๊ฒฝ์šฐ๋Š” cache miss๋ฏ€๋กœ answer += 5

โ‘ก ์žˆ๋Š” ๊ฒฝ์šฐ buffer๋ฅผ ๊ฐ€์žฅ ๋†’์€ ์šฐ์„  ์ˆœ์œ„๋กœ ์—…๋ฐ์ดํŠธ -> remove()๋กœ ์ฐธ์กฐ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹น ์š”์†Œ ์ œ๊ฑฐํ•œ ํ›„ append()๋กœ ๋งจ ๋’ท์ž๋ฆฌ์— ์œ„์น˜์‹œํ‚ค๊ธฐ

๐Ÿ’ฌ ์ฐธ๊ณ ๋กœ, popleftํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ stack ๋ณด๋‹ค๋Š” deque ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

 

๐ŸŽซcode )

from collections import deque

def solution(cacheSize, cities):
    answer = 0
    buffer = deque()

    if cacheSize == 0:
        return 5 * len(cities)

    for city in cities:
        city = city.lower()
        if city not in buffer:
            if len(buffer) == cacheSize:
                buffer.popleft()
            buffer.append(city)
            answer += 5
        else:
            buffer.remove(city)
            buffer.append(city)
            answer += 1

    return answer

 

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : programmers.co.kr/learn/courses/30/lessons/17680

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ์บ์‹œ

3 [Jeju, Pangyo, Seoul, NewYork, LA, Jeju, Pangyo, Seoul, NewYork, LA] 50 3 [Jeju, Pangyo, Seoul, Jeju, Pangyo, Seoul, Jeju, Pangyo, Seoul] 21 2 [Jeju, Pangyo, Seoul, NewYork, LA, SanFrancisco, Seoul, Rome, Paris, Jeju, NewYork, Rome] 60 5 [Jeju, Pangyo, S

programmers.co.kr

์บ์‹œ

์ง€๋„๊ฐœ๋ฐœํŒ€์—์„œ ๊ทผ๋ฌดํ•˜๋Š” ์ œ์ด์ง€๋Š” ์ง€๋„์—์„œ ๋„์‹œ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•˜๋ฉด ํ•ด๋‹น ๋„์‹œ์™€ ๊ด€๋ จ๋œ ๋ง›์ง‘ ๊ฒŒ์‹œ๋ฌผ๋“ค์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ฝ์–ด ๋ณด์—ฌ์ฃผ๋Š” ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋‹ค.
์ด ํ”„๋กœ๊ทธ๋žจ์˜ ํ…Œ์ŠคํŒ… ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š” ์–ดํ”ผ์น˜๋Š” ์„œ๋น„์Šค๋ฅผ ์˜คํ”ˆํ•˜๊ธฐ ์ „ ๊ฐ ๋กœ์ง์— ๋Œ€ํ•œ ์„ฑ๋Šฅ ์ธก์ •์„ ์ˆ˜ํ–‰ํ•˜์˜€๋Š”๋ฐ, ์ œ์ด์ง€๊ฐ€ ์ž‘์„ฑํ•œ ๋ถ€๋ถ„ ์ค‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์˜ ์‹คํ–‰์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
์–ดํ”ผ์น˜๋Š” ์ œ์ด์ง€์—๊ฒŒ ํ•ด๋‹น ๋กœ์ง์„ ๊ฐœ์„ ํ•˜๋ผ๊ณ  ๋‹ฆ๋‹ฌํ•˜๊ธฐ ์‹œ์ž‘ํ•˜์˜€๊ณ , ์ œ์ด์ง€๋Š” DB ์บ์‹œ๋ฅผ ์ ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ ๊ฐœ์„ ์„ ์‹œ๋„ํ•˜๊ณ  ์žˆ์ง€๋งŒ ์บ์‹œ ํฌ๊ธฐ๋ฅผ ์–ผ๋งˆ๋กœ ํ•ด์•ผ ํšจ์œจ์ ์ธ์ง€ ๋ชฐ๋ผ ๋‚œ๊ฐํ•œ ์ƒํ™ฉ์ด๋‹ค.

์–ดํ”ผ์น˜์—๊ฒŒ ์‹œ๋‹ฌ๋ฆฌ๋Š” ์ œ์ด์ง€๋ฅผ ๋„์™€, DB ์บ์‹œ๋ฅผ ์ ์šฉํ•  ๋•Œ ์บ์‹œ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์‹คํ–‰์‹œ๊ฐ„ ์ธก์ • ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ ํ˜•์‹

  • ์บ์‹œ ํฌ๊ธฐ(cacheSize)์™€ ๋„์‹œ์ด๋ฆ„ ๋ฐฐ์—ด(cities)์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • cacheSize๋Š” ์ •์ˆ˜์ด๋ฉฐ, ๋ฒ”์œ„๋Š” 0 โ‰ฆ cacheSize โ‰ฆ 30 ์ด๋‹ค.
  • cities๋Š” ๋„์‹œ ์ด๋ฆ„์œผ๋กœ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ, ์ตœ๋Œ€ ๋„์‹œ ์ˆ˜๋Š” 100,000๊ฐœ์ด๋‹ค.
  • ๊ฐ ๋„์‹œ ์ด๋ฆ„์€ ๊ณต๋ฐฑ, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž ๋“ฑ์ด ์—†๋Š” ์˜๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋„์‹œ ์ด๋ฆ„์€ ์ตœ๋Œ€ 20์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

  • ์ž…๋ ฅ๋œ ๋„์‹œ์ด๋ฆ„ ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ, ์ด ์‹คํ–‰์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

์กฐ๊ฑด

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

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

 

๋ฐ˜์‘ํ˜•