【发布时间】:2020-09-20 03:24:30
【问题描述】:
最近,在一次竞争性编码考试中,我得到了这个问题 -
一个好的数字是数字之和可以被 5 整除的数字。例如 - 5 (5), 14 (1+4), 19(1+9), 23(2+3)
问题是 - 为您提供一个整数 n 和另一个整数 k,您必须找到大于 n 的第 k 个好数。
约束 - 1<k<10^9
样本测试 1 -
input: n = 6, k = 5
output: 32
Explanation: After 6 good numbers are - 14, 19, 23, 28, 32 (5th is 32)
样本测试 2 -
input: n = 5, k = 1
output: 14
Explanation: 5 is 1st good number but we need greater than 5 so ans is 14
我已经尝试过使用本机方法,即对于每个大于 n 的数字,检查它是否良好并循环直到我找到 k 个好的数字,这是我的代码 -
def solve(n,k):
n+=1
count = 0
while count<k:
if sum(map(int,str(n)))%5==0:
count+=1
n+=1
return n-1
但是上面的代码给了我 TLE,如何以更好的时间复杂度来做,我在网上搜索了类似的问题,但找不到,帮助。
【问题讨论】:
-
添加了数字动态规划的答案。
标签: algorithm math data-structures