Description#
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.
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 <= 105
tires[i].length == 2
1 <= fi, changeTime <= 105
2 <= ri <= 105
1 <= numLaps <= 1000
Solutions#
Solution 1#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| class Solution {
public int minimumFinishTime(int[][] tires, int changeTime, int numLaps) {
final int inf = 1 << 30;
int[] cost = new int[18];
Arrays.fill(cost, inf);
for (int[] e : tires) {
int f = e[0], r = e[1];
int s = 0, t = f;
for (int i = 1; t <= changeTime + f; ++i) {
s += t;
cost[i] = Math.min(cost[i], s);
t *= r;
}
}
int[] f = new int[numLaps + 1];
Arrays.fill(f, inf);
f[0] = -changeTime;
for (int i = 1; i <= numLaps; ++i) {
for (int j = 1; j < Math.min(18, i + 1); ++j) {
f[i] = Math.min(f[i], f[i - j] + cost[j]);
}
f[i] += changeTime;
}
return f[numLaps];
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| class Solution {
public:
int minimumFinishTime(vector<vector<int>>& tires, int changeTime, int numLaps) {
int cost[18];
memset(cost, 0x3f, sizeof(cost));
for (auto& e : tires) {
int f = e[0], r = e[1];
int s = 0;
long long t = f;
for (int i = 1; t <= changeTime + f; ++i) {
s += t;
cost[i] = min(cost[i], s);
t *= r;
}
}
int f[numLaps + 1];
memset(f, 0x3f, sizeof(f));
f[0] = -changeTime;
for (int i = 1; i <= numLaps; ++i) {
for (int j = 1; j < min(18, i + 1); ++j) {
f[i] = min(f[i], f[i - j] + cost[j]);
}
f[i] += changeTime;
}
return f[numLaps];
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| func minimumFinishTime(tires [][]int, changeTime int, numLaps int) int {
const inf = 1 << 30
cost := [18]int{}
for i := range cost {
cost[i] = inf
}
for _, e := range tires {
f, r := e[0], e[1]
s, t := 0, f
for i := 1; t <= changeTime+f; i++ {
s += t
cost[i] = min(cost[i], s)
t *= r
}
}
f := make([]int, numLaps+1)
for i := range f {
f[i] = inf
}
f[0] = -changeTime
for i := 1; i <= numLaps; i++ {
for j := 1; j < min(18, i+1); j++ {
f[i] = min(f[i], f[i-j]+cost[j])
}
f[i] += changeTime
}
return f[numLaps]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| function minimumFinishTime(tires: number[][], changeTime: number, numLaps: number): number {
const cost: number[] = Array(18).fill(Infinity);
for (const [f, r] of tires) {
let s = 0;
let t = f;
for (let i = 1; t <= changeTime + f; ++i) {
s += t;
cost[i] = Math.min(cost[i], s);
t *= r;
}
}
const f: number[] = Array(numLaps + 1).fill(Infinity);
f[0] = -changeTime;
for (let i = 1; i <= numLaps; ++i) {
for (let j = 1; j < Math.min(18, i + 1); ++j) {
f[i] = Math.min(f[i], f[i - j] + cost[j]);
}
f[i] += changeTime;
}
return f[numLaps];
}
|