Leetcode 1515 Solution

This article provides solution to leetcode question 1515 (find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k)

https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k

Solution

class Solution:
    def findMinFibonacciNumbers(self, k: int) -> int:
        a = [1, 1]
        while a[-1] <= k:
            a.append(a[-1] + a[-2])

        ans = 0
        index = len(a) - 1
        while index >= 0 and k > 0:
            if k >= a[index]:
                k -= a[index]
                ans += 1
            index -= 1
        return ans