๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2018 KAKAO BLIND RECRUITMENT] [1์ฐจ] ์บ์‹œ(Lv.1) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 9.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/17680

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

ํ’€์ด

1. deque ์‚ฌ์šฉ

from collections import deque

 

  • deque๋Š” ์–‘๋ฐฉํ–ฅ์—์„œ ๋น ๋ฅธ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋ฅผ ์ง€์›ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์บ์‹œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•  ๋•Œ ๋น ๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

2. ์บ์‹œ ํฌ๊ธฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

if cacheSize == 0:
    return len(cities) * 5
  • ์บ์‹œ ํฌ๊ธฐ๊ฐ€ 0์ผ ๋•Œ๋Š” ์บ์‹œ๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๋ชจ๋“  ๋„์‹œ์—์„œ ๋ฌด์กฐ๊ฑด ์บ์‹œ ๋ฏธ์Šค๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋งŒํผ 5๋ฅผ ๊ณฑํ•ด ๋ฏธ์Šค ์‹œ๊ฐ„์˜ ์ดํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์บ์‹œ๊ฐ€ ์—†๋‹ค๋Š” ์ƒํ™ฉ์„ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ณ , ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค.

3. ์บ์‹œ ๋ฐ ๋„์‹œ ์ด๋ฆ„์˜ ๋Œ€์†Œ๋ฌธ์ž ํ†ต์ผ ์ฒ˜๋ฆฌ

cache = deque(maxlen=cacheSize)  # ์บ์‹œ ํฌ๊ธฐ ์„ค์ •
  • deque์— maxlen์„ ์„ค์ •ํ•˜์—ฌ ์บ์‹œ๊ฐ€ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๋„˜๊ธฐ๋ฉด ์ž๋™์œผ๋กœ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์บ์‹œ์˜ ํฌ๊ธฐ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  deque๊ฐ€ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.
for city in cities:
    city = city.lower()
  • ๋„์‹œ ์ผ๋ฏ€์˜ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด lower() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“  ๋„์‹œ ์ด๋ฆ„์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

4. ์บ์‹œ ํžˆํŠธ ๋ฐ ๋ฏธ์Šค ์ฒ˜๋ฆฌ

if city in cache:
    answer += 1  # ์บ์‹œ ํžˆํŠธ
    cache.remove(city)  # ๊ธฐ์กด ์œ„์น˜์—์„œ ์ œ๊ฑฐ ํ›„ ์ตœ์‹ ํ™”
else:
    answer += 5  # ์บ์‹œ ๋ฏธ์Šค
  • ์บ์‹œ ํžˆํŠธ ์ฒ˜๋ฆฌ
    • ๋„์‹œ๊ฐ€ ์บ์‹œ์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์บ์‹œ ํžˆํŠธ๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‹คํ–‰ ์‹œ๊ฐ„์„ 1๋งŒ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์บ์‹œ์˜ ๊ธฐ์กด ์œ„์น˜์—์„œ ํ•ด๋‹น ๋„์‹œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ตœ์‹  ํ•ญ๋ชฉ์œผ๋กœ ๋‹ค์‹œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ cache.remove(city)๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์บ์‹œ ๋ฏธ์Šค ์ฒ˜๋ฆฌ
    • ๋„์‹œ๊ฐ€ ์บ์‹œ์— ์—†์œผ๋ฉด ์บ์‹œ ๋ฏธ์Šค๋กœ ๊ฐ„์ฃผ๋˜๋ฉฐ, ์‹คํ–‰์‹œ๊ฐ„์„ 5 ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” ๋Œ์˜ remove ์—†์ด ๋ฐ”๋กœ ์บ์‹œ์— ๋„์‹œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

4. ์บ์‹œ ๊ด€๋ฆฌ

cache.append(city)
  • ๋„์‹œ๊ฐ€ ์บ์‹œ์— ์—†๊ฑฐ๋‚˜, ์บ์‹œ ํžˆํŠธ๋กœ ์ธํ•ด ์ œ๊ฑฐ๋œ ํ›„ ๋‹ค์‹œ ์บ์‹œ์˜ ๋งจ ๋’ค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, deque์˜ maxlen์ด ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์บ์‹œ๊ฐ€ ๊ฐ€๋“ ์ฐผ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ํ•ญ๋ชฉ์ด ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.
  • deque๊ฐ€ ์ž๋™์œผ๋กœ ํ•ญ๋ชฉ์„ ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ ์บ์‹œ ํฌ๊ธฐ ์ œํ•œ์„ ์ดˆ๊ณผํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข…์ฝ”๋“œ

from collections import deque

def solution(cacheSize, cities):
    if cacheSize == 0:
        return len(cities) * 5  # ์บ์‹œ ํฌ๊ธฐ๊ฐ€ 0์ผ ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด ์บ์‹œ ๋ฏธ์Šค

    answer = 0
    cache = deque(maxlen=cacheSize)  # deque์— maxlen์„ ์„ค์ •ํ•˜์—ฌ ์ž๋™์œผ๋กœ ๊ด€๋ฆฌ

    for city in cities:
        city = city.lower()  # ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์• ๊ธฐ

        if city in cache:
            answer += 1  # ์บ์‹œ ํžˆํŠธ
            cache.remove(city)  # ๊ธฐ์กด ์œ„์น˜์—์„œ ์ œ๊ฑฐ ํ›„
        else:
            answer += 5  # ์บ์‹œ ๋ฏธ์Šค
        
        cache.append(city)  # ์บ์‹œ์˜ ๋์— ์ถ”๊ฐ€ (์ž๋™์œผ๋กœ ์˜ค๋ž˜๋œ ํ•ญ๋ชฉ ์ œ๊ฑฐ)

    return answer

 

728x90
๋ฐ˜์‘ํ˜•