Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Fast Train by Moff
def fast_train(sections):
speedlimit = []
for l, s in sections:
speedlimit.extend([s] * l)
result = dict(value=len(speedlimit))
def schedule(speed, result):
if len(speed) >= result['value']:
return
way = sum(speed)
v = speed[-1]
if v == 0 or way > len(speedlimit) or any(speedlimit[way - 1 - i] < v for i in range(v)):
return
if way == len(speedlimit) and v == 1:
if len(speed) < result['value']:
result['value'] = len(speed)
for ds in (1, 0, -1):
schedule(speed + [speed[-1] + ds], result)
schedule([1], result)
return result['value']
Oct. 26, 2018