Minimum Time to Finish the Race Problem
Description
LeetCode Problem 2188.
You are given a 0-indexed 2D integer array tires where tires[i] = [f_i, r_i] indicates that the i^th tire can finish its x^th successive lap in f_i * r_i^((x-1)) seconds.
- For example, if
f_i = 3andr_i = 2, then the tire would finish its1^stlap in3seconds, its2^ndlap in3 * 2 = 6seconds, its3^rdlap in3 * 2^2 = 12seconds, etc.
You are also given an integer changeTime and an integer numLaps.
The race consists of numLaps laps and you may start the race with any tire. You have an unlimited supply of each tire and after every lap, you may change to any given tire (including the current tire type) if you wait changeTime seconds.
Return the minimum time to finish the race.
Example 1:
Input: tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4
Output: 21
Explanation:
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
The minimum time to complete the race is 21 seconds.
Example 2:
Input: tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
Output: 25
Explanation:
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
The minimum time to complete the race is 25 seconds.
Constraints:
1 <= tires.length <= 10^5tires[i].length == 21 <= f_i, changeTime <= 10^52 <= r_i <= 10^51 <= numLaps <= 1000
Solution
Approach: Best Cost for k Consecutive Laps, Then DP
A tire gets worse every lap, and after a point it is cheaper to change it than to keep using it, so no tire is worth using for more than about 17 laps in a row. For each tire, compute the total time of using it for 1, 2, ... laps while a lap is no slower than changing tires, and keep the best cost[j] over all tires for j consecutive laps. Then let f[i] be the minimum time for i laps: f[i] = min(f[i - j] + cost[j]) + changeTime, with f[0] = -changeTime so that the first stint pays no change.
Complexity: O(tires * 17 + numLaps * 17) time and O(numLaps) space.
from math import inf
class Solution:
def minimumFinishTime(
self, tires: List[List[int]], changeTime: int, numLaps: int
) -> int:
cost = [inf] * 18
for f, r in tires:
i, s, t = 1, 0, f
while t <= changeTime + f:
s += t
cost[i] = min(cost[i], s)
t *= r
i += 1
f = [inf] * (numLaps + 1)
f[0] = -changeTime
for i in range(1, numLaps + 1):
for j in range(1, min(18, i + 1)):
f[i] = min(f[i], f[i - j] + cost[j])
f[i] += changeTime
return f[numLaps]