Skip to content
LC-0509 Easy LeetCode

509. Fibonacci Number

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 73% Topics: Math, Dynamic Programming, Recursion, Memoization
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

import itertools


class Solution(object):
    def fib(self, N):
        """
        :type N: int
        :rtype: int
        """
        def matrix_expo(A, K):
            result = [[int(i==j) for j in xrange(len(A))] \
                      for i in xrange(len(A))]
            while K:
                if K % 2:
                    result = matrix_mult(result, A)
                A = matrix_mult(A, A)
                K /= 2
            return result

        def matrix_mult(A, B):
            ZB = zip(*B)
            return [[sum(a*b for a, b in itertools.izip(row, col)) \
                     for col in ZB] for row in A]

        T = [[1, 1],
             [1, 0]]
        return matrix_mult([[1, 0]], matrix_expo(T, N))[0][1]  # [a1, a0] * T^N


# Time:  O(n)
# Space: O(1)
class Solution2(object):
    def fib(self, N):
        """
        :type N: int
        :rtype: int
        """
        prev, current = 0, 1
        for i in xrange(N):
            prev, current = current, prev + current,
        return prev

Solution from kamyu104/LeetCode-Solutions · MIT