当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- Web界面元素的测试
- (中)苹果有开源,但又怎样呢?
- Customize the gateway filter factory on the specified route
- Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
- Online and offline problems
- P问题、NP问题、NPC问题、NP-hard问题详解
- 职场进阶指南:大厂人必看书籍推荐
- GTSAM中李群的运用
- MySQL之基础知识
猜你喜欢

【eolink】PC客户端安装

Postman核心功能解析-参数化和测试报告

nodejs实现微博第三方登录

The latest 2022 review of "graph classification research"

Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai

Database isolation level

LeetCode 739. 每日温度

Gtest之TEST宏的用法

【Postman】测试(Tests)脚本编写和断言详解

Summary of anomaly detection methods
随机推荐
properties文件
P问题、NP问题、NPC问题、NP-hard问题详解
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
IP day 16 VLAN MPLS configuration
GTSAM中李群的運用
测试周期被压缩?教你9个方法去应对
Left matching principle of joint index
Commodity price visualization
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
Idea new UI usage
GTSAM中李群的运用
功能安全之故障(fault),错误(error),失效(failure)
LeetCode 731. 我的日程安排表 II
[postman] the monitors monitoring API can run periodically
Resttemplate and feign realize token transmission
[course notes] Compilation Principle
Cognitive introspection
【eolink】PC客户端安装
全链路压测:构建三大模型