<span>leetcode-1414 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K</span>

Given an integer k, return the minimum number of Fibonacci numbers whose sum is equal to k. The same Fibonacci number can be used multiple times.

The Fibonacci numbers are defined as:

F1 = 1
F2 = 1
Fn = Fn-1 + Fn-2 for n > 2.
It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to k.

输入输出实例:

Input: k = 7
Output: 2 
Explanation: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... 
For k = 7 we can use 2 + 5 = 7.

代码:

 1 class Solution:
 2     def findMinFibonacciNumbers(self, k: int) -> int:
 3         F1, F2 = 1, 1
 4         while F2 <= k:
 5             F2, F1 = F2 + F1, F2
 6         num = 0
 7         while F1 > 0:
 8             if k > F2:
 9                 num += 1
10                 k -= F2
11             F2, F1 = F1, F2 - F1
12         return num

 

  

全部评论

相关推荐

牛客539033066号:放心吧,这里面一大半都不会去面试的,剩下一半面过了最后还是回拒,实际上免笔试的那些bg的人,没多少愿意去这些岗位,薪资水平在那里
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务