@lru_cache

  • lru_cache - ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’μ„ κΈ°μ–΅ν•΄μ€Œ. lru_cacheλŠ” λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©λ˜λŠ”λ°, μ›ν•˜λŠ” ν•¨μˆ˜μ— 이 λ°μ½”λ ˆμ΄ν„°λ₯Ό 달아 μ£Όλ©΄ λ°˜ν™˜κ°’μ„ μ•Œμ•„μ„œ μ €μž₯ν•΄μ€€λ‹€.

from functools import lru_cache

@lru_cache(maxsize=None)
def fibo(n):
    if n < 2:
        return n
    return fibo(n - 1) + fibo(n - 2)

print(fibo(int(input())))

Last updated