๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [1์ฐจ] ์บ์‹œ

๐ŸŽฎinspirer9 2023. 2. 20. 23:12
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

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

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

์ œํ•œ ์‚ฌํ•ญ

์ž…๋ ฅ ํ˜•์‹

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

์ถœ๋ ฅ ํ˜•์‹

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

์กฐ๊ฑด

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

์ž…์ถœ๋ ฅ ์˜ˆ

์บ์‹œํฌ๊ธฐ(cacheSize) ๋„์‹œ์ด๋ฆ„(cities) ์‹คํ–‰์‹œ๊ฐ„
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", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"] 52
2 ["Jeju", "Pangyo", "NewYork", "newyork"] 16
0 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA"] 25

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์—†์Œ. ๋Œ€์‹  ํ•ด์„ค์ด ์žˆ์Œ.

ํ’€์ด

  • ๋จผ์ € ์บ์‹œ ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” LRU(Least Recently Used)๊ฐ€ ๋ญ”์ง€ ํ™•์ธํ•œ๋‹ค.
    • ๊ฐ€์žฅ ์˜ค๋žœ ์‹œ๊ฐ„ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ํŽ˜์ด์ง€๋ฅผ ๊ต์ฒดํ•˜๋Š” ์šด์˜์ฒด์ œ์˜ ํŽ˜์ด์ง€ ๊ต์ฒด ์ •์ฑ… ์ค‘ ํ•˜๋‚˜
    • Q ์Šคํƒ€์ผ ์ž๋ฃŒ๊ตฌ์กฐ์ธ๊ฐ€?
    • ์บ์‹œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•ž์—์„œ ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰.
    • ์—†์œผ๋ฉด ์•ž์ชฝ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ฐ€์–ด๋„ฃ๊ณ ... ์บ์‹œ ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฑด ๋ฒ„๋ฆฌ๋Š” ๋ฐฉ์‹.
  • LRU ์บ์‹œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ๋นผ๋ฉด์„œ ์นด์šดํŒ…์„ ํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๊ฑด๊ฐ€?
    • ์ง„์งœ ๊ตฌํ˜„ ๋ฌธ์ œ์ธ๊ฐ€? ์„ค๋งˆ...? ์ •๋ง?
  • ๊ทธ๋ž˜ ๋ฌด์ง€์„ฑ ๊ตฌํ˜„ ๊ฐ€์ฆˆ์•„!
from collections import deque

def solution(cacheSize, cities):
    answer = 0
    cache = deque(["" for _ in range(cacheSize)])
    for city in cities:
        city = city.lower()
        if cache.count(city): #cache hit
            index = cache.index(city)
            answer += 1
        else: # cache miss
            cache.appendleft(city)
            cache.pop()
            answer += 5
    return answer
  • ํ— ์ง„์ง ๊ฐ€?
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	3, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	50
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	3, ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	21
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 3
์ž…๋ ฅ๊ฐ’ ใ€‰	2, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	60
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 4
์ž…๋ ฅ๊ฐ’ ใ€‰	5, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	52
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 5
์ž…๋ ฅ๊ฐ’ ใ€‰	2, ["Jeju", "Pangyo", "NewYork", "newyork"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	16
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 6
์ž…๋ ฅ๊ฐ’ ใ€‰	0, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	25
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ์ฑ—์  ๊ฐ€์ฆˆ์•„!
    • ๋ญ์•ผ? ์‹คํŒจํ•˜๋Š”๋””?
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.98MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (126.20ms, 17.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.1MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	์‹คํŒจ (0.21ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.38ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	์‹คํŒจ (0.34ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	์‹คํŒจ (0.84ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	์‹คํŒจ (0.64ms, 10.2MB)
  • ์•„... ์•Œ์•˜๋‹ค.
    • ์บ์‹œ ํžˆํŠธ ํ–ˆ๋”๋ผ๋„, ์บ์‹œ ์œ„์น˜๋ฅผ ๋‹ค์‹œ ์•ž์œผ๋กœ ์˜ฎ๊ฒจ์ค˜์•ผ ํ•˜๋Š”๊ตฌ๋‚˜?
    • ์ด๋Ÿฌ๋‹ˆ ๊ฐœ๋Š๋ฆฌ์ง€ ใ…ก.ใ…ก;
from collections import deque

def solution(cacheSize, cities):
    answer = 0
    cache = deque(["" for _ in range(cacheSize)])
    for city in cities:
        city = city.lower()
        if cache.count(city): #cache hit
            index = cache.index(city)
            cache.remove(city)
            cache.appendleft(city)
            answer += 1
        else: # cache miss
            cache.appendleft(city)
            cache.pop()
            answer += 5
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (88.27ms, 17.5MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.1MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.42ms, 10MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.71ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.99ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (1.03ms, 10.3MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ์บ์‹œ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฏธ๋ฆฌ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํœด๋จผ.
    • cache.index(city)๋กœ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํœด๋จผ
    • ๋ณด๊ณ  ๋ฐฐ์šฐ์‹ญ์‹œ์˜ค. ํœด๋จผ.
def solution(cacheSize, cities):
    import collections
    cache = collections.deque(maxlen=cacheSize)
    time = 0
    for i in cities:
        s = i.lower()
        if s in cache:
            cache.remove(s)
            cache.append(s)
            time += 1
        else:
            cache.append(s)
            time += 5
    return time
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.99MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (85.69ms, 17.5MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.04ms, 10MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.1MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.54ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.59ms, 10.3MB)
  • ๋˜ ๋‹ค๋ฅธ ๊ณ ์ˆ˜, ๋””ํ ์•ˆ์จ๋„ ๋น ๋ฅด๋„ค...
def solution(cacheSize, cities):
    answer = 0
    stack = []
    for x in map(lambda x: x.lower(), cities):
        if x in stack:
            answer += 1
            stack.remove(x)
        else:
            answer += 5
        stack += [x]

        if len(stack) > cacheSize:
            del stack[0]

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (69.60ms, 17.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.59ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.71ms, 10.1MB)
  • ์นด์นด์˜ค ๋ฌธ์ œ๋“ค์€ ๋ฌธ์ œ ์„ค๋ช…์ด ์ด์ƒํ•˜๋‹ค.
728x90
๋ฐ˜์‘ํ˜•