当前位置:网站首页>模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
2022-07-06 06:15:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
1414. 和为 K 的最少斐波那契数字数目
给你数字 k ,请你返回和为 k 的斐波那契数字的最少数目,其中,每个斐波那契数字都可以被使用多次。
斐波那契数字定义为:
F1 = 1
F2 = 1
Fn = Fn-1 + Fn-2 , 其中 n > 2 。
数据保证对于给定的 k ,一定能找到可行解。
示例 1:
输入:k = 7
输出:2
解释:斐波那契数字为:1,1,2,3,5,8,13,……
对于 k = 7 ,我们可以得到 2 + 5 = 7 。
示例 2:
输入:k = 10
输出:2
解释:对于 k = 10 ,我们可以得到 2 + 8 = 10 。
示例 3:
输入:k = 19
输出:3
解释:对于 k = 19 ,我们可以得到 1 + 5 + 13 = 19 。
提示:
1 <= k <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
f = [1,1]
while f[-1]<k:f.append(f[-1]+f[-2])
res,i = 0,len(f)-1
while k:
if k>=f[i]:
k-=f[i]
res+=1
i-=1
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Manhattan distance and Manhattan rectangle - print back font matrix
- leaflet 地图
- IDEA 新UI使用
- [postman] test script writing and assertion details
- [C language] string left rotation
- 二维码的前世今生 与 六大测试点梳理
- [postman] collections - run the imported data file of the configuration
- Basic knowledge of error
- 多线程应用的测试与调试
- Construction and integration of Zipkin and sleuth for call chain monitoring
猜你喜欢
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
[postman] collections configuration running process
P问题、NP问题、NPC问题、NP-hard问题详解
IPv6 comprehensive experiment
Customize the gateway filter factory on the specified route
Understanding of processes and threads
G - Supermarket
[web security] nodejs prototype chain pollution analysis
Nodejs realizes the third-party login of Weibo
Manhattan distance sum - print diamond
随机推荐
LeetCode 731. 我的日程安排表 II
Function of contenttype
Resttemplate and feign realize token transmission
[web security] nodejs prototype chain pollution analysis
Detailed explanation of P problem, NP problem, NPC problem and NP hard problem
Significance of unit testing
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Function of activation function
【Postman】动态变量(也称Mock函数)
Construction and integration of Zipkin and sleuth for call chain monitoring
P问题、NP问题、NPC问题、NP-hard问题详解
曼哈顿距离和-打印菱形
把el-tree选中的数组转换为数组对象
JWT-JSON WEB TOKEN
[course notes] Compilation Principle
黑猫带你学UFS协议第4篇:UFS协议栈详解
Thoughts on data security (Reprint)
公司視頻加速播放
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
[eolink] PC client installation