You are given a 0-indexed 2D integer array tires where tires[i] = [fi, ri] indicates that the ith tire can finish its xth successive lap in `fi
ri(x-1)` seconds.
For example, if fi = 3 and ri = 2, then the tire would finish its 1st lap in 3 seconds, its 2nd lap in 3 * 2 = 6 seconds, its 3rd lap in 3 * 22 = 12 seconds, 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.
Input: tires =[[2,3],[3,4]], changeTime =5, numLaps =4Output: 21Explanation:
Lap 1: Start with tire 0 and finish the lap in2 seconds.Lap 2: Continue with tire 0 and finish the lap in2*3=6 seconds.Lap 3: Change tires to a new tire 0for5 seconds and then finish the lap in another 2 seconds.Lap 4: Continue with tire 0 and finish the lap in2*3=6 seconds.Total time =2+6+5+2+6=21 seconds.The minimum time to complete the race is21 seconds.
Input: tires =[[1,10],[2,2],[3,4]], changeTime =6, numLaps =5Output: 25Explanation:
Lap 1: Start with tire 1 and finish the lap in2 seconds.Lap 2: Continue with tire 1 and finish the lap in2*2=4 seconds.Lap 3: Change tires to a new tire 1for6 seconds and then finish the lap in another 2 seconds.Lap 4: Continue with tire 1 and finish the lap in2*2=4 seconds.Lap 5: Change tires to tire 0for6 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 is25 seconds.
For each tire, the lap time increases exponentially. It’s optimal to use a tire for several consecutive laps until it’s better to change. Precompute the minimum time to finish k consecutive laps with any tire, then use DP to find the minimum total time for numLaps.
For each tire, precompute the minimum time to run k consecutive laps (without changing tires), up to a reasonable limit (when lap time exceeds changeTime + best first lap).
For each k, keep the minimum time among all tires.
Use DP: dp[i] = minimum time to finish i laps.
For each i, try all possible k (consecutive laps) and update dp[i] = min(dp[i], dp[i-k] + minTime[k] + changeTime).
The answer is dp[numLaps] minus changeTime (no change after last lap).
classSolution {
publicintminimumFinishTime(int[][] tires, int changeTime, int numLaps) {
int INF = (int)1e9;
int[] minTime =newint[numLaps+1];
Arrays.fill(minTime, INF);
for (int[] t : tires) {
int f = t[0], r = t[1];
long time = 0, curr = f;
for (int k = 1; k <= numLaps && curr <= f + changeTime; k++) {
time += curr;
minTime[k]= Math.min(minTime[k], (int)time);
curr *= r;
}
}
int[] dp =newint[numLaps+1];
Arrays.fill(dp, INF);
dp[0]= 0;
for (int i = 1; i <= numLaps; i++) {
for (int k = 1; k <= i; k++) {
if (minTime[k]< INF)
dp[i]= Math.min(dp[i], dp[i-k]+ minTime[k]+ changeTime);
}
}
return dp[numLaps]- changeTime;
}
}
classSolution {
funminimumFinishTime(tires: Array<IntArray>, changeTime: Int, numLaps: Int): Int {
val INF = 1e9.toInt()
val minTime = IntArray(numLaps+1) { INF }
for (t in tires) {
var f = t[0]
var r = t[1]
var time = 0Lvar curr = f
for (k in1..numLaps) {
if (curr > f + changeTime) break time += curr
minTime[k] = minOf(minTime[k], time.toInt())
curr *= r
}
}
val dp = IntArray(numLaps+1) { INF }
dp[0] = 0for (i in1..numLaps) {
for (k in1..i) {
if (minTime[k] < INF)
dp[i] = minOf(dp[i], dp[i-k] + minTime[k] + changeTime)
}
}
return dp[numLaps] - changeTime
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defminimumFinishTime(self, tires: list[list[int]], changeTime: int, numLaps: int) -> int:
INF = int(1e9)
minTime = [INF] * (numLaps+1)
for f, r in tires:
time, curr =0, f
for k in range(1, numLaps+1):
if curr > f + changeTime:
break time += curr
minTime[k] = min(minTime[k], time)
curr *= r
dp = [INF] * (numLaps+1)
dp[0] =0for i in range(1, numLaps+1):
for k in range(1, i+1):
if minTime[k] < INF:
dp[i] = min(dp[i], dp[i-k] + minTime[k] + changeTime)
return dp[numLaps] - changeTime