Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
DFS + recursion solution in Clear category for Fast Train by kdim
from typing import List
def fast_train(sections: List[List[int]]) -> int:
def DFS(train, t, tm):
overspeed = lambda x: x < 1 or any(i > j for i, j in zip(train, speeds))
s, v = len(train), train[-1]
if s > end or t > tm or overspeed(v):
return end
if s == end and v == 1:
return t
for d in (1, 0, -1):
tm = min(tm, DFS(train + [v + d] * (v + d), t + 1, tm))
return tm
speeds = sum(([j] * i for i, j in sections), [])
end = sum(i for i, j in sections)
return DFS([1], 1, end)
Feb. 3, 2021
Comments: