3
self, tires: List[List[int]], changeTime: int, numLaps: int5
# by observation, we can try to find out the optimal usage within certain numLaps7
# the optimal usage of this lap = min(change tire , no change)8
# dp(laps) = min( dp(laps-1)+dp(1) + dp(laps-2)+dp(2) + ...)10
# we don't want to use tires too many laps, which will create unrealistic single lap time11
# we can evaluate single lap time by using changeTime <= 100000 and r >= 212
# x = minimal continously laps13
# single lap time = 1*2^x <= 100000 -> x can't go more than 1915
tires = list(set([(t1, t2) for t1, t2 in tires]))17
[(-1, -1) for _ in range(min(limit, numLaps) + 1)]18
for _ in range(len(tires))21
for i in range(len(tires)):22
for j in range(1, min(limit, numLaps) + 1): # lap 1 to numLaps24
memo[i][j] = (tires[i][0], tires[i][0]) # total time, lap time28
memo[i][j - 1][1] * tires[i][1]29
) # cost of continuously use tire this lap30
memo[i][j] = (memo[i][j - 1][0] + tmp, tmp)35
return min(memo[i][1][0] for i in range(len(tires)))39
min(memo[i][laps][0] for i in range(len(tires)))45
# e.g. change tire at this lap and see if it'll be faster -> dp(laps-1) + changeTime + dp(1)46
# check all previous laps: dp(a) + changeTime + dp(b) until a < b47
for j in range(1, laps):52
if ta + tb + changeTime < best_time:53
best_time = ta + tb + changeTime