山姆和凯利是编程伙伴。凯利决定更多地练习,因为山姆最初领先。他们每个人每天都要解决一些问题。找出凯利比萨姆解决了更多问题的最小天数。如果Kelly不能超过,则返回-1。
示例:
samDaily = 3
kellyDaily = 5
difference = 5最初,山姆比凯利解决了更多的差异问题。每天,他们分别解决samDaily和kellyDaily问题。第一天:samSolved = difference + samDaily =5+3 =8 kellySolved = kellyDaily =5
第2天:samSolved =8+3= 11 kellySolved =5+5= 10第3天:samSolved = 11 +3= 14 kellySolved = 10 +5= 15萨姆比凯利先解决5个问题,他们每天解决3到5个问题。山姆将在第一天后仅领先3分,第二天后1分,而凯利将在第三天超过山姆。
对如何实现这一点有什么想法吗?最好是用Python编写。
发布于 2020-06-25 06:34:57
谢谢宁。
我认为你分享的这个解决方案中有一个错误,我错误地将其提升了。
解决方案应该是:
def days_needed(kellyDaily, samDaily, difference):
x = difference / (kellyDaily - samDaily)
if x <= 0: return -1
return int(x+1)发布于 2020-08-29 19:01:26
这是优化的,并且所有测试用例都通过了:
class Result
{
public static int minNum(int samDaily, int kellyDaily, int difference)
{
// No catching up because samDaily is ahead (positive P) and kellyDaily cannot catch up.
if (kellyDaily <= samDaily)
{
return -1;
}
return difference/(kellyDaily - samDaily) + 1;
}
}发布于 2020-06-14 11:32:59
它完全是关于数学的,而不是算法或数据结构。假设x是天数。在x天数之后:
samSolved = difference + x*samDaily
kellySolved = x*kellyDaily对于kellySolved > samSolved,我们需要找到正整数x的最小值。
x*kellyDaily > difference + x*samDaily
x > (samDaily-kellyDaily)/differencePthon代码:
def days_needed(kellyDaily, samDaily, difference):
x = (samDaily-kellyDaily)/difference
if x <= 0: return -1
return int(x+1)https://stackoverflow.com/questions/62367113
复制相似问题