<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

 

  

全部评论

相关推荐

2024-11-11 09:31
香港中文大学 前台
Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务