python - 在 Python 中解决问题以在团队中分配多个项目
问题描述
我最近在一个在线编码平台上偶然发现了一个解决问题的问题。
一位经理想向他的员工分发巧克力。实际上,分发巧克力并不容易。每个员工小队都有严格的资历等级,必须尊重 - 否则员工会反抗。
为了避免叛乱,您必须遵循 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、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)
我得到了正确的答案,但在隐藏的测试用例中仍然失败。如果有人可以帮助我找到问题所在,我将不胜感激?
解决方案
推荐阅读
- stored-procedures - 试图从雪花中的过程返回变量值。它正在出错
- http - 我如何在 Parasoft SOATest 的响应对象中引用从邮递员发送的请求标头/正文元数据?
- elasticsearch - 使用 Heartbeat 的弹性正常运行时间监视器——kibana 中缺少几个监视器
- javascript - 根据分配给先前对象的先前值,为对象中的键分配值
- android - 为什么应用程序没有安装
- c# - 为什么在从 C# 创建的进程中运行 bash 命令时我的 $PATH 不同?
- php - Docker,将服务名称从 PHP 更改为不同的返回 503 Service Unavailable
- ios - Xcode 12:无法在 iOS 设备上启动远程服务“com.apple.debugserver”
- python - Python 硒被阻塞
- javascript - 如何使用 laravel 表中的特定列显示数据库中的列表