我最近在网上编码平台上偶然发现了一个解决问题的问题。
经理想把巧克力分发给他的员工。实际上散发巧克力并不容易。每个员工队伍都有严格的资历等级,必须予以尊重-否则员工会反抗。
为了避免反抗,您必须遵循4条关键规则:
1. The most junior employee (with the least seniority) gets exactly 1 chocolate. (There will always be at least 1 employee on a team.)
2. An employee will revolt if the person who ranks immediately above them gets more than double the number of chocolates as they do.
3. An employee will revolt if the amount of chocolates given to their next two subordinates combined is more than the number of chocolates they get. (Note that the two most junior employees won't have two subordinates, so this rule doesn't apply to them. The 2nd most junior employee would require at least as many chocolates as the most junior employee.)
4. You can always find more employees to pay - the Manager has plenty of employees. If there is enough chocolate leftover such that another employee could be added as the most senior while obeying the other rules, you must always add and pay that employee.
请注意,您可能无法分发所有的巧克力。单个LAMB不能再细分。也就是说,所有的小伙子们都必须获得正整数的巧克力。
编写一个称为solution(total_chocolates)的函数,其中total_ Chocolates是您要除的讲义中巧克力的整数。它应该返回一个整数,该整数表示可以共享巧克力的最小和最大雇员人数之间的差额(即,分别对您所付的巧克力和雇员尽可能慷慨和尽可能小气),同时仍然服从所有以上规则,以避免起义。例如,如果您有10颗巧克力,并且尽可能地慷慨,则您只能支付3名员工(按照升序排列,分别有1、2和4颗巧克力),而如果您太小气,则可以支付4员工(1、2、3巧克力)。因此,solution(10)应该返回4-3 = 1。
为使事情变得有趣,经理更改了巧克力支出的大小。您可以期望total_chocolates始终是小于10亿(10 ^ 9)的正整数。
我想出了以下解决方案:
def solution(total_chocolates):
if not isinstance(total_chocolates, int) or total_lambs<=1 or total_lambs >= 1000000000:
return 0
else:
F1=[1,1]
F2=[1]
for i in range(1,total_chocolates):
if sum(F1) < total_chocolates:
F1.append(F1[-1]+F1[-2]) #fibonacci series from 1
if sum(F1) > total_chocolates:
F1.pop()
LEN_F1 = len(F1)
if sum(F2) < total_chocolates:
F2.append(F2[-1] * 2) #1,2,4,8,16.. series
if sum(F2) > total_chocolates:
F2.pop()
LEN_F2 = len(F2)
return (LEN_F1-LEN_F2)
我得到了正确的答案,但在隐藏的测试用例中仍然失败。如果有人可以帮助我找到问题所在,我将不胜感激。