我有一个使用贪婪算法解决汽车加油问题的小问题。
问题介绍
您将前往距离您家乡数英里的另一个城市。您的汽车在加满油箱的情况下最多可以行驶数英里,而您从加满油箱开始。沿途有加油站,距离 stop1 stop2 。. . ,stopN 从你的家乡城市。最少需要多少次补充?
输入:
950
400
4
200 375 550 750
Run Code Online (Sandbox Code Playgroud)
输出:
2
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试过的
def car_fueling(dist,miles,n,gas_stations):
num_refill, curr_refill, last_refill = 0,0,0
while curr_refill <= n:
last_refill = curr_refill
while (curr_refill <= n-1) & (gas_stations[curr_refill + 1] - gas_stations[last_refill] <= miles):
curr_refill += 1
if curr_refill == last_refill:
return -1
if curr_refill <= n:
num_refill += 1
return num_refill
Run Code Online (Sandbox Code Playgroud)
我面临的问题是什么
在声明中
while (curr_refill <= n-1) & (gas_stations[curr_refill + 1] - gas_stations[last_refill] <= miles)
Run Code Online (Sandbox Code Playgroud)
我收到错误IndexError: …
自动完成功能需要建议 json 格式的函数名称,如下所示:
{
"id": 260,
"title": "p_active(Power: number, PowerAngle: number)",
"type": "PR",
"category": "AL",
"structure": "p_active(Power: number, PowerAngle: number)"
}
Run Code Online (Sandbox Code Playgroud)
这里,p_active是一个函数名称,需要包含在建议中。
ag-grid ×1
algorithm ×1
angular ×1
greedy ×1
html ×1
javascript ×1
list ×1
python ×1
python-3.x ×1