当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
猜你喜欢

CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全

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
![[untitled]](/img/5d/028b9d19e9a2b217f40198d4631db2.png)
[untitled]

Database isolation level

D - How Many Answers Are Wrong

【eolink】PC客户端安装

GTSAM中李群的运用

D - How Many Answers Are Wrong

数学三大核心领域概述:代数

Configuring OSPF GR features for Huawei devices
随机推荐
数字三角形模型 AcWing 1015. 摘花生
php使用redis实现分布式锁
JWT-JSON WEB TOKEN
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
【Postman】Collections-运行配置之导入数据文件
D - How Many Answers Are Wrong
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
Cognitive introspection
LeetCode 1200. 最小绝对差
GTSAM中李群的运用
[eolink] PC client installation
技术分享 | 常见接口协议解析
Réflexions sur la sécurité des données (réimpression)
二维码的前世今生 与 六大测试点梳理
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
G - Supermarket
The latest 2022 review of "graph classification research"
F - true liars (category and search set +dp)
黑猫带你学UFS协议第4篇:UFS协议栈详解
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word