728x90

문제 19. (2021-05-19)

피보나치 수열(최적화)

공간 복잡도 O(1)로 fib_optimized 함수를 작성하세요.

 

 

❧ 테스트 셋

def fib_optimized(n):
    # Code


# Test
print(fib_optimized(16))
print(fib_optimized(53))
print(fib_optimized(213))

 

❧ 출력 예시

987
53316291173
146178119651438213260386312206974243796773058

 


❧ 정답

def fib_optimized(n):
    current = 1
    previous = 0

    # update
    for i in range(1, n):
        current, previous = current + previous, current

    return current
    

# Test
print(fib_optimized(16))
print(fib_optimized(53))
print(fib_optimized(213))

 

728x90

 

728x90